Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: Assign Output of Regex to Array (newbie)

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

in reply to Assign Output of Regex to Array (newbie)

Are you looking for push?

while( <FILE> ) { push @array, $_ if /Run as/ .. /^ /; }


Replies are listed 'Best First'.
Re^2: Assign Output of Regex to Array (newbie)
by justsomeguy (Novice) on Sep 25, 2013 at 19:31 UTC
    Well, that's a start. Do I need a chomp and split as well? The data is space-delimited.

      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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1055734]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2018-04-19 22:15 GMT
Find Nodes?
    Voting Booth?