Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

OUTPUT DATA TO FILE - SIMPLE PROBLEM

Status
Not open for further replies.

DJpennywhistle

Programmer
Jun 1, 2000
32
0
0
US
Visit site
I am parsing a data file for parameters in an if loop. Something like:<br><br>if($file_line_starts_with_input)<br>{<br>&nbsp;&nbsp;&nbsp;#get first string as $value1<br><br>&nbsp;&nbsp;&nbsp;#get first value as $value2<br>}<br><br>I want to output these to a file called params.opt. So, inside my if loop I used <br><br>$params = &quot;param.opt&quot;;<br><br>open (OUTPUT, &quot;&gt;&quot; . $params);<br><br>print OUTPUT (&quot;$value1=$value2\n&quot;);<br><br>but as you, being better at perl than me, will know the lines over write each other giving:<br><br>### start of file #####<br>last_value1 = last_value2<br><br>### end of file ########<br><br>&nbsp;I want them to follow each other in a list so all parsed data can be used.<br>can anybody help me??<br><br>thanks <p>Gordon Bell<br><a href=mailto:gordon.bell@xilinx.com>gordon.bell@xilinx.com</a><br><a href= > </a><br>
 
Put the &quot;open&quot; outside of the loop.&nbsp;&nbsp;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.<br><br>For example:<br><FONT FACE=monospace><br>open(MYOUT, &quot;&gt;$params&quot;);<br><br>while(<i>read data from file</i>) {<br>&nbsp;&nbsp;&nbsp;&nbsp;<i>Do something with data</i><br>&nbsp;&nbsp;&nbsp;&nbsp;print OUTPUT (&quot;$value1=$value2\n&quot;);<br>}<br><br>close(MYOUT);
 
Oops - I forgot to also mention that if you ever want to append data to an existing file, use &quot;&gt;&gt;&quot; instead of &quot;&gt;&quot; in the open statement.<br><br>For example:<br><FONT FACE=monospace><br>open(MYOUT, &quot;&gt;&gt;$My_Output_File&quot;);<br></font>
 
Thanks again Andy pity I can't give you something back my Perl's not great - having any trouble with your golf swing? <p>Gordon Bell<br><a href=mailto:gordon.bell@xilinx.com>gordon.bell@xilinx.com</a><br><a href= > </a><br>
 
No problem Gordon - glad to be of assistance :)&nbsp;&nbsp;I don't play much golf - I've got a bit of a dodgy back, I'm afraid :(&nbsp;&nbsp;I've always fancied learning how to play guitar though... ;^)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top