Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Split file output into array of arrays

by colwellj (Monk)
on Feb 16, 2010 at 23:38 UTC ( [id://823576]=note: print w/replies, xml ) Need Help??


in reply to Split file output into array of arrays

Scott,

Try something akin to this;
N.B code is untested and I've probably screwed up the syntax somewhere.
my $arraycount=0; my @array; while(<INPUT>){ #New record so new top level item if($_ =~ /^-/){ $arraycount++; }else{ #force array scalar to be a lower level array. push @{$array[$arraycount]}, $_; } }
or you could do both your steps at once.
my $arraycount=0; my @array; while(<INPUT>){ #New record so new top level item if($_ =~ /^-/){ $arraycount++; }else{ #force array scalar to be a lower level HASH. my ($hkey,$hdet) = split/:/,$_; ${$array[$arraycount]}{$hkey} = $hdet; } }

Replies are listed 'Best First'.
Re^2: Split file output into array of arrays
by GrandFather (Saint) on Feb 16, 2010 at 23:49 UTC

    Keeping a count of elements in an array in Perl has rather a bad smell. Instead you could:

    my @records; while (<DATA>) { if ($_ =~ /^-/) { # New record push @records, []; next; } next if !@records; push @{$records[-1]}, $_; }

    True laziness is hard work

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (6)
As of 2024-04-23 20:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found