×
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!
  • Students Click Here

*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

Jobs

OUTPUT DATA TO FILE - SIMPLE PROBLEM

OUTPUT DATA TO FILE - SIMPLE PROBLEM

OUTPUT DATA TO FILE - SIMPLE PROBLEM

(OP)
I am parsing a data file for parameters in an if loop. Something like:

if($file_line_starts_with_input)
{
   #get first string as $value1

   #get first value as $value2
}

I want to output these to a file called params.opt. So, inside my if loop I used

$params = "param.opt";

open (OUTPUT, ">" . $params);

print OUTPUT ("$value1=$value2\n");

but as you, being better at perl than me, will know the lines over write each other giving:

### start of file #####
last_value1 = last_value2

### end of file ########

 I want them to follow each other in a list so all parsed data can be used.
can anybody help me??

thanks

Gordon Bell
gordon.bell@xilinx.com

RE: OUTPUT DATA TO FILE - SIMPLE PROBLEM

Put the "open" outside of the loop.  That way you won't keep overwriting the data each time you write out a new line, and you won't have the overhead or re-opening the same file many times.

For example:

open(MYOUT, ">$params");

while(read data from file) {
    Do something with data
    print OUTPUT ("$value1=$value2\n");
}

close(MYOUT);

RE: OUTPUT DATA TO FILE - SIMPLE PROBLEM

Oops - I forgot to also mention that if you ever want to append data to an existing file, use ">>" instead of ">" in the open statement.

For example:

open(MYOUT, ">>$My_Output_File");

RE: OUTPUT DATA TO FILE - SIMPLE PROBLEM

(OP)
Thanks again Andy pity I can't give you something back my Perl's not great - having any trouble with your golf swing?

Gordon Bell
gordon.bell@xilinx.com

RE: OUTPUT DATA TO FILE - SIMPLE PROBLEM

No problem Gordon - glad to be of assistance :)  I don't play much golf - I've got a bit of a dodgy back, I'm afraid :(  I've always fancied learning how to play guitar though... ;^)

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