Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: convert array of hash into formatted one

by anonymized user 468275 (Curate)
on Aug 05, 2015 at 17:14 UTC ( #1137553=note: print w/replies, xml ) Need Help??

in reply to convert array of hash into formatted one

Looks like you need a sort function or block and to keep the previous (sorted) case in an up-scope variable, e.g.
my $prevCase = ''; for my $i (sort caseThenId (0..$#$var1) { my $case = $var1->[$i]{case}; print "$case:"\n" if $case ne $prevCase; $prevCase = $case; print $var1->[$i]{id} . ") " . $var1->[$i]{name} . ", " . $var1->[$i]{degree} . "\n"; } sub caseThenId { $var1->[$a]{case} cmp $var1->[$b]{case} or $var1->[$a]{id} cmp $var1->[$b]{id}; }
Update: $var1 should be a reference to your array in the above example.

One world, one people

Replies are listed 'Best First'.
Re^2: convert array of hash into formatted one
by derby (Abbot) on Aug 05, 2015 at 18:01 UTC

    Why would you need a sort? It appears the array is already in the order the OP wants.

      Two reasons: 1, the OP did not specify what sorting had been applied. 2, the four records shown might be accidentally in order, or the example might not fit the actual data when applied, so it might help the OP to see what the sort algorithm should be.

      One world, one people

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2023-01-27 07:45 GMT
Find Nodes?
    Voting Booth?

    No recent polls found