The basic structure of your code is efficient as it only loops throught the AoH once (you sort of described it as twice, but in fact once). To loop through twice is not a good idea for performance, if things can be done within one loop. Just to make that loop a little bit simpler.
use Data::Dumper; my $AoH = [ { name => "Bob", gender => "M" }, { name => "Sue", gender => "F" }, { name => "Mike", gender => "M" } ]; my ($men, $women); map {(push @{($_->{"gender"} eq "M") ? $men : $women}, $_)} @$AoH; print Dumper($men); print Dumper($women);
Side note: even with your code, there was no need for you to remember the last index of the arrays. You can always push, or use $#array, so $m and $w can be removed.
In reply to Re: Assigning AoH pairs by value other AoH's
by pg
in thread Assigning AoH pairs by value other AoH's
by bradcathey
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |