Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by justsomeguy (Novice)
on Sep 25, 2013 at 19:31 UTC ( #1055737=note: print w/ replies, xml ) Need Help??


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

Well, that's a start. Do I need a chomp and split as well? The data is space-delimited.


Comment on Re^2: Assign Output of Regex to Array (newbie)
Re^3: Assign Output of Regex to Array (newbie)
by davido (Archbishop) on Sep 25, 2013 at 22:00 UTC

    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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (8)
As of 2014-12-27 10:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls