Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^3: Assign Output of Regex to Array (newbie)

by davido (Archbishop)
on Sep 25, 2013 at 22:00 UTC ( #1055753=note: print w/ replies, xml ) Need Help??


in reply to Re^2: Assign Output of Regex to Array (newbie)
in thread Assign Output of Regex to Array (newbie)

If I were dealing with a space delimited file of simple characteristics, and I wanted to insert it into a multidimensional array, along with a /start/ .. /end/ conditional, I might do it like this:

while( <$fh> ) { chomp; next unless length; if( /start/ .. /end/ ) { push @array, [ split /\s+/, $_ ]; } }

Your definition of the start and end pattern are difficult for me to guess without knowing more about the input data, but you were probably on the right track to begin with there.

If there's any risk that the input file could contain things like spaces embedded within quoted fields, or other more complex constructs that are challenging for split, then you would want to shift to using Text::CSV, specifying space as the delimiter rather than commas.

Oh, one last thought; if the input data has fixed-width fields, then splitting on space becomes less important; you could just unpack based on a fixed unpack specification.


Dave


Comment on Re^3: Assign Output of Regex to Array (newbie)
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1055753]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (15)
As of 2014-07-30 12:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (231 votes), past polls