perl - How do I extract all array values from a hash of hashes -
newbie here. aplogies if missing details.
in perl 5
i have file kind of looks this
precedence = 2 new york new bailiwick of jersey florida precedence = 3 kings essex dade precedence = 1 brooklyn newark miami i have no problem looping through file , creating $var holds value of precedence , array (@tmp) holds lines until next "section" (precedence = x)
i need force sections final array in order of preference
so
print @final; results in
brooklyn ..... new york ..... kings ..... note: never know in advance how many sections there or how many lines each section have
i thought perhapes create hash of hashes , set each array in hoh
push @{ $hash{"section_2"} }, @tmp ; but didnt know
a) if there problem reusing @tmp array each time load section in (after blanking @ origin of each loop)
b) couldnt figure out how values in array in key "section_2" , force them @final
of course of study there may improve approach.
an hoh makes no sense. utilize hoa if expect wide variance in precedence levels (1, 1000000, 1000000000),
my $precedence = 0; %data; while (<>) { chomp; if (/precedence\s*=\s*([0-9]+)\z/) { $precedence = $1; next; } force @{ $data{$precedence} }, $_; } @final = map @{ $data{$_} }, sort { $a <=> $b } keys %data; but aoa improve fit.
my $precedence = 0; @data; while (<>) { chomp; if (/precedence\s*=\s*([0-9]+)\z/) { $precedence = $1; next; } force @{ $data[$precedence] }, $_; } @final = map @$_, grep $_, @data; arrays perl hash
No comments:
Post a Comment