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!

Andybo - How do I make the info $My_string

Status
Not open for further replies.

DJpennywhistle

Programmer
Jun 1, 2000
32
US
I see what you mean with the split function but how can I pick out this line from a file with various information (no lines the same as this one though) and make it $My_string??<br><br>Thanks Andy <p>Gordon Bell<br><a href=mailto:gordon.bell@xilinx.com>gordon.bell@xilinx.com</a><br><a href= > </a><br>
 
OK, say you've got a file like:<br><FONT FACE=monospace><br>line 1<br>line 2<br>some data ¦ 100ns ¦ 200 ns ¦ 1<br>line 3 <br>line 4<br></font><br>You only want to select lines that contain the data.&nbsp;&nbsp;I'm going to assume a couple of things... 1 - that the data file is small enough&nbsp;&nbsp;to be comfortably held in memory, and 2 - that the data lines always have 3 &quot;¦&quot; characters, and that the 2nd and 3rd fields data ends with &quot;ns&quot;.<br><br>The following should then work:<br><FONT FACE=monospace><br>#!/usr/bin/perl<br><br># Read the contents of the file into an array.<br>open(INPUT, &quot;&lt;/path/to/data/file&quot;);<br>while (&lt;INPUT&gt;) {<br>&nbsp;&nbsp;&nbsp;&nbsp;push(@File_Array, $_);<br>}<br>close(INPUT);<br><br># Now use grep to look for lines that match our<br># data line pattern.<br>#<br># .* matches any character 0 or more times<br># \s* matches any whitespace char 0 or more times<br># \d+ns matches any string of digits immediately followed<br>#&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;by the chars &quot;ns&quot;.<br># \¦ match &quot;¦&quot; chars.<br>@Data_Lines = grep(/.*\¦\s*\d+ns\s*\¦\s*\d+ns\s*\¦\s*\d*/, @File_Array);<br><br># @Data_Lines now holds an array of lines containing the data<br>foreach $Line ( @Data_Lines ) {<br>&nbsp;&nbsp;&nbsp;&nbsp;@Items = split(/\¦/, $Line);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;<i>Do processing on fields here...</i><br><br>}<br></font><br><br>I've done a quick test, and this seems to work OK, though you might need to tweak it a little for your exact requirements.
 
Thanks Andy - you are some man!!! <p>Gordon Bell<br><a href=mailto:gordon.bell@xilinx.com>gordon.bell@xilinx.com</a><br><a href= > </a><br>
 
You can simplify AndyBo's program a bit at the expense of debuggability:<br><FONT FACE=monospace><br>while (&lt;INPUT&gt;) {<br>&nbsp;&nbsp;&nbsp;&nbsp;push(@File_Array, $_);<br>}<br></font><br>collapses to<br><FONT FACE=monospace><br>@File_Array = &lt;INPUT&gt;;<br></font><br>I use this regularly. If you are sure of your regular expression, you can even add in the next line. Instead of reading the file into an array, then greping the data lines out of the array, you can grep the input directly:<br><FONT FACE=monospace><br>open(INPUT, &quot;&lt;/path/to/data/file&quot;);<br>@File_Array = &lt;INPUT&gt;<br>close(INPUT);<br><br>@Data_Lines = grep(/.*\¦\s*\d+ns\s*\¦\s*\d+ns\s*\¦\s*\d*/, @File_Array);<br></font><br>becomes<br><FONT FACE=monospace><br>open(INPUT, &quot;&lt;/path/to/data/file&quot;);<br>@Data_Lines = grep(/.*\¦\s*\d+ns\s*\¦\s*\d+ns\s*\¦\s*\d*/, &lt;INPUT&gt;);<br>close(INPUT);<br></font><br><br>But as I said at the start, collapsing this much happens at the expense of debuggability. If I have problems with the script not picking up lines that it should then I expand back to a loop so I can see what is happening.<br><br>- Kai.<br><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top