Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

How would I write this using map?

by Plankton (Vicar)
on Jan 12, 2009 at 23:31 UTC ( [id://735833]=perlquestion: print w/replies, xml ) Need Help??

Plankton has asked for the wisdom of the Perl Monks concerning the following question:

I know this should dome using map ... but I just don't understand how to use map
sub add2array { my $ref2array = shift; my $obj = new THING; for my $thing ( sort keys $obj->things ) { push ( @{$ref2array} $thing ); } }
How would it look if I used map? I know this isn't it ...
$ref2array = map { push @{$_} } sort keys $obj->things;
... or is it?

Replies are listed 'Best First'.
Re: How would I write this using map?
by kwaping (Priest) on Jan 12, 2009 at 23:55 UTC
    Using map, it would look like this:
    push @$ref2array, map $_, sort keys %{$obj->things};
    ... which could then be distilled to:
    push @$ref2array, sort keys %{$obj->things};

    ---
    It's all fine and dandy until someone has to look at the code.
Re: How would I write this using map?
by monarch (Priest) on Jan 13, 2009 at 00:38 UTC
    The map is used for performing an action on every item in an array, with the result of that action returned.

    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:

    map { push( @{$ref2array}, $_ ) } ( sort( keys( %{$obj->things() } );
    which is identical to
    foreach ( sort( keys( %{$obj->things} ) ) ) { push( @{$ref2array}, $_ ); }
    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).

    What if you wanted to, say, store an uppercase-version of all the keys into the array? Then map becomes very useful:

    push( @{$ref2array}, map { uc($_) } ( sort( keys( %{$obj->things()} ) ) ) );
    This is much quicker than, but functionally the same as:
    my @uppercasekeys; foreach ( sort( keys( %{$obj->things()} ) ) ) { push( @uppercasekeys, uc( $_ ) ); } push( @{$ref2array}, @uppercasekeys );
Re: How would I write this using map?
by jwkrahn (Abbot) on Jan 13, 2009 at 00:39 UTC
    sub add2array { my $obj = new THING; push @{ $_[ 0 ] }, sort keys $obj->things; }
Re: How would I write this using map?
by trwww (Priest) on Jan 13, 2009 at 04:13 UTC

    Hello,

    I don't think your code works. In keys $obj->things, keys probably complains about not being given a hash.

    Anyhoo, You don't need map here:

    $things = [ sort keys %{$obj->things} ];

    A common reason you'd want map is if ->things returned a list of objects and you needed to call a method on all of them and collect the output:

    # an array ref of all the things' names $names = [ map $_->name, $obj->things ];

    hth,

      The code most likely works. Without knowing the object internals you really can not say what things the object contains and in what data structure. A hash is the most natural way to store data with objects so I would assume that things returns a hash.
      --
      seek $her, $from, $everywhere if exists $true{love};
        I can't see how this would work. I tried to create some mock code:
        use strict; { package MyObj; sub new { my %obj; bless( \%obj, $_[0] ); return( \%obj ); } sub items { my @things = ( 'key1' => 'v1', key2' => 'v2', ); return( @things ); } } my $myobj = MyObj->new(); print( "$_\n" ) foreach ( sort keys( $myobj->items() ) );
        but kept getting this error:
        Type of arg 1 to keys must be hash (not subroutine entry) at /tmp/deleteme.pl line 25, near ") ) "

        You cant return a hash from a function. Only lists and scalars.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2024-04-20 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found