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

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.

    -derby
      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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2022-05-17 04:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (65 votes). Check out past polls.

    Notices?