Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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.


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

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2015-10-08 16:49 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (221 votes), past polls