in reply to How would I write this using map?
In this case it appears that you want to push (in order) each key from the hash reference $obj->things.
As you're not performing a transformation the quickest thing to do is to push the list directly onto the array:
push( @{$ref2array}, sort( keys( %{$obj->things()} ) ) );
However map could be used to re-write your loop as:
which is identical tomap { push( @{$ref2array}, $_ ) } ( sort( keys( %{$obj->things() } );
However using map here isn't so common because nothing is being done with the output of map. You're merely using map to iterate through each item (which is a valid use, though).foreach ( sort( keys( %{$obj->things} ) ) ) { push( @{$ref2array}, $_ ); }
What if you wanted to, say, store an uppercase-version of all the keys into the array? Then map becomes very useful:
This is much quicker than, but functionally the same as:push( @{$ref2array}, map { uc($_) } ( sort( keys( %{$obj->things()} ) ) ) );
my @uppercasekeys; foreach ( sort( keys( %{$obj->things()} ) ) ) { push( @uppercasekeys, uc( $_ ) ); } push( @{$ref2array}, @uppercasekeys );
|
---|