×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

OUTPUT DATA TO FILE - STILL WON'T WORK!!!!

OUTPUT DATA TO FILE - STILL WON'T WORK!!!!

OUTPUT DATA TO FILE - STILL WON'T WORK!!!!

(OP)
Okay, so I want to output this list of data into a file.

while(<INPUT_FILE>)
{
       #do various parsing

      open (OUT_FILE, ">$params"); #open params.opt file
                                   #outside loop so it will                                    #list all values


      if(/(?:^input)s*(.*)=(.*)/)    # If a line starts                           {                              # 'input' I want it

            $value1 = $1;
            $value2 = $2;

            #should write each time to OUT_FILE
            print OUT_FILE ("$value1=$value2\n");

       }

       close(OUT_FILE);

What I want is my params.opt file to look like:

$value1=$value2; #from 1st loop
$value1=$value2; #from 2nd loop
$value1=$value2; #from 3rd loop
$value1=$value2; #from 4th loop  etc.

But the code above doesn't work. I keep getting an empty params.opt file - what is wrong??



Gordon Bell
gordon.bell@xilinx.com

RE: OUTPUT DATA TO FILE - STILL WON'T WORK!!!!

It looks to me like you still have the open statement in the wrong place.  Each time you get a line from your INPUT_FILE handle, you are re-opening your OUT_FILE handle.  The open automatically closes the handle if was already open and then re-opens it.  This completely overwrites anything that was there.   The last thing you get from INPUT_FILE is probably EOF.  So, the last loop through the while results in your re-opening the OUT_FILE and writing (printing) EOF to the OUT_FILE.  All you need to do is move the open statement to outside of the while loop.  You only want to open the OUT_FILE once.

while(<INPUT_FILE>)
{
    #do various parsing
    open (OUT_FILE, ">$params");WRONG PLACE
    if(/(?:^input)s*(.*)=(.*)/)    # If a line starts
        {      # 'input' I want it
        $value1 = $1;
        $value2 = $2;
        #should write each time to OUT_FILE
        print OUT_FILE ("$value1=$value2\n");
        } # end of pattern match
} # end while INPUT_FILE
close(OUT_FILE);

open (OUT_FILE, ">$params");CORRECT PLACE
while(<INPUT_FILE>)
{
    #do various parsing
    if(/(?:^input)s*(.*)=(.*)/)    # If a line starts
        {      # 'input' I want it
        $value1 = $1;
        $value2 = $2;
        #should write each time to OUT_FILE
        print OUT_FILE ("$value1=$value2\n");
        } # end of pattern match
} # end while INPUT_FILE
close(OUT_FILE);


Additionally, your use of $1 and $2 appears to be off by one slot.  I think you want to be putting $2 in $value1 and $3 in value2.

I think I sense a little frustration setting in...... hang in there, your making progress....




keep the rudder amid ship and beware the odd typo

RE: OUTPUT DATA TO FILE - STILL WON'T WORK!!!!

(OP)
Good weekemd boating?

You are right about me getting frustrated - I have been stuck with this one for days. The thing is though - I can't move the open out of the loop because during my parsing I create new folders move to them and in each one open a params.opt file to output the data to.

while(<INPUT_FILE>)
                          {
                                 # DO VAVIOUS PARSING: EXTRACT FILENAME, MKDIR OF THAT FILENAME, CHDIR                                     TO THE NEW FILE - THEN OPEN PARAMS.OPT

                                open (OUT_FILE, ">$params"); #open params.opt file
                                                             #outside loop so it will                                    #list all values


                                if(/(?:^input)s*(.*)=(.*)/)    # If a line starts                           {                  

                                      $value1 = $1;
                                      $value2 = $2;

                                      #should write each time to OUT_FILE
                                      print OUT_FILE ("$value1=$value2\n");

                                 }

                                 close(OUT_FILE);

Thanks again for your help

Gordon Bell
gordon.bell@xilinx.com

RE: OUTPUT DATA TO FILE - STILL WON'T WORK!!!!

(OP)
Its cool. I worked out how to do it using an array.

Thanks once again though

Gordon Bell
gordon.bell@xilinx.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close