http://qs1969.pair.com?node_id=1137553


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