Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

taking sections out of a file and put it in an array

by juo (Curate)
on Dec 06, 2002 at 08:38 UTC ( [id://218008]=perlquestion: print w/replies, xml ) Need Help??

juo has asked for the wisdom of the Perl Monks concerning the following question:

How can I easily and fast take sections out of a huge asci file and put it in an array. See example below. When a line starts with $FILES and ends with $ then everything between I want to put in an array.

text dsfasdfdsf dfasd $FILES p1agd064, tn00015, tnh0015, pex35v, psm35v, pko35, pki12, layer +id,labl175, labl165, NONAM000, pv046025, pki25, pex46v, psm46v, pko46 +, sm00137,pa0805, ia0805, ma030055, logo164, NONAM001, NONAM002, NONA +M003, pvia10sp,$ text dfada

Edit by dws to add <code> tags

Replies are listed 'Best First'.
Re: taking sections out of a file and put it in an array
by davorg (Chancellor) on Dec 06, 2002 at 08:55 UTC
    my @arr; while (<FILE>) { push @arr, $1 if /\$FILES(.*?)\$/; }
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      starts with $FILES and ends with $ :
      my @arr; while (<FILE>) { push @arr, $1 if /^\$FILES(.*?)\$$/; }
      (inserted '^' and '$' to mark start and end)

        Check juo's example, there will be more data after the $ so you'll need to leave of the second $ (as davorg did) or it won't match.

        open DATA, "junk.txt" or die $!; my @arr; while (<DATA>) { # add the extra $ and it won't print anything push @arr, $1 if /\$FILES(.*?)\$/; } close DATA; print $_, "\n" for @arr;

        Try it out.

        Update: I think I misinterpreted Chief of Chaos's post. If you want to ensure there's no data before the $FILE or after the $ then he is correct.

        Update: If you want each CSV as an individual array element, substitute the push line above with:

        push @arr, split(/, /, $1) if /\$FILES(.*?)\$/;

        Otherwise they'll all read into one element.

      The problem is that it assumes that everything is on one line while the text is on multiple lines so it has to keep putting it in the array unless the line contains the $. I have tried to use while in the while loop but it gets out of memory. Is their an easy way when a line is found to keep adding the lines in the array untill something else is found in a next line.

Re: taking sections out of a file and put it in an array
by Chief of Chaos (Friar) on Dec 06, 2002 at 09:15 UTC
    May be you can post the next time with code tags.
    Does the file looks like that ?
    text dsfasdfdsf dfasd $FILES p1agd064, tn00015, tnh0015, pex35v, psm35v, pko35, pki12, layer +id,labl175, labl165, NONAM000, pv046025, pki25, pex46v, psm46v, pko46 +, sm00137,pa0805, ia0805, ma030055, logo164, NONAM001, NONAM002, NONA +M003, pvia10sp,$ text dfada

      Yes it does. Did not know about the code tags. Thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://218008]
Approved by cLive ;-)
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-26 07:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found