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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need to output values to a new file....

Status
Not open for further replies.

DJpennywhistle

Programmer
Jun 1, 2000
32
US
I have two variables in memory and I want to output them to a new file called type.opt . One is a string and the other is a value. They are in a loop and change with each transition. They have to be output in the style<br><br>string1=value1&nbsp;&nbsp;&nbsp;#loop 1<br>string2=value2&nbsp;&nbsp;&nbsp;#loop 2<br>string3=value3&nbsp;&nbsp;&nbsp;#loop 3<br>string4=value4&nbsp;&nbsp;&nbsp;#loop 4 etc.<br><br>Thanks for any suggestions <p>Gordon Bell<br><a href=mailto:gordon.bell@xilinx.com>gordon.bell@xilinx.com</a><br><a href= > </a><br>
 
If you are doing CGI stuff and are using CGI.pm, then open an output file (type.opt) and use the 'save' method....like.<br><br><b><br># with vars in memory.<br>open(OPF,&quot;&gt;type.opt&quot;) ¦¦ &complain(&quot;Failed to open output file, $!&quot;);<br>$query-&gt;save(OPF);<br>close OPF;<br></b><br><br>If you are not doing CGI with CGI.pm.<br><b><br>%hash_of_pairs = ('string1','value1','string2','value2','string3','value3');<br>open (OPF,&quot;&gt;type.opt&quot;) ¦¦ die &quot;Failed to open output file, $!\n&quot;;<br>foreach $key (keys(%hash_of_pairs))<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;# print key and value to output file.<br>&nbsp;&nbsp;&nbsp;&nbsp;print OPF &quot;$key = $hash_of_pairs{$key}\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>close OPF;<br></b><br><br><i>beware the odd typo</i>
 
Another helpful hint is that you do not have to use the letters OPF, you can use anything like FILE, or FILEHANDLE, anything works like that.<br><br>Hope I could help.
 
goBoating - The 2nd piece of script you gave me seemed to be what I needed. The only problem was with the line <br>%hash_of_pairs = ('string1','value1','string2','value2','string3','value3');<br><br>because the number of strings and values is variable and of no maximum length.<br><br>Thanks for your help anyway - and if you haven't gone boating today you might be able to help me&nbsp;&nbsp;on this one - cheers!! <p>Gordon Bell<br><a href=mailto:gordon.bell@xilinx.com>gordon.bell@xilinx.com</a><br><a href= > </a><br>
 
That is still not that clear. <br><br>I have two variables $string and $value. Both change on each loop and there is a variable number of loops. Each time there is a new loop the variables change and I want to make a list of these variables in a .opt file.<br><br>Sorry if I'm repeating myself but my first description was not very clear.<br><br>Thanks again <p>Gordon Bell<br><a href=mailto:gordon.bell@xilinx.com>gordon.bell@xilinx.com</a><br><a href= > </a><br>
 
I couched the previous post in <i>hash</i> terms to emulate the series of paired values you listed.&nbsp;&nbsp;The hash could be any iterative (looping) data source.&nbsp;&nbsp;A hash or an array or a looping file read/split behavior could supply a similar series of values to feed the output logic.<br>.....as vikter points out, OPF is my own convention<br>&nbsp;&nbsp;&nbsp;&nbsp;<b>OPF</b> for <b>O</b>ut<b>P</b>ut<b>F</b>ile<br>similarly - IPF for InPutFile - you can call the handle what ever makes sense to you<br><br># get a handle on your OutPutFile (OPF)<br><b>open (OPF,&quot;&gt;type.opt&quot;) ¦¦ die &quot;Failed to open output file, $!\n&quot;;</b><br><br># If you are reading some input stream (e.g. reading a file line by line)......<br><b>open (IPF,&quot;&lt;someInputFile&quot;) ¦¦ die &quot;Failed to open input file, $!\n&quot;;<br>while ($line = &lt;IPF&gt;)<br>&nbsp;&nbsp;&nbsp;&nbsp;{<br>&nbsp;&nbsp;&nbsp;&nbsp;</b>#split or pattern match the elements you want to store.<br>&nbsp;&nbsp;&nbsp;&nbsp;# If your file looks like <i>lastName¦phoneExtension</i> - pipe delimited<br>&nbsp;&nbsp;&nbsp;&nbsp;<b>($name,$extension) = split(/\¦/,$line); </b>split the line on a pipe, '¦'<b><br>&nbsp;&nbsp;&nbsp;&nbsp;</b># print name and extension to OPF<br>&nbsp;&nbsp;&nbsp;&nbsp;<b>print OPF &quot;$name = $extension\n&quot;;<br>&nbsp;&nbsp;&nbsp;&nbsp;}<br>close IPF;<br>close OPF;<br></b><br><br><i>beware the odd typo</i>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top