Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

use of 'map'

by Madam (Sexton)
on Jul 14, 2005 at 11:09 UTC ( [id://474825]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Perl Monks, I have list in which i need to concat values of the list with "=>" .Following is the code for it.
while( my ($k, $v) = each %list ) { push(@arr,$k,($v = "=>".$v)); }
but i want to know whether i can use "map" for this one, as i am not much aware of "map" usage. Thanks in advance, Madam

Replies are listed 'Best First'.
Re: use of 'map'
by blazar (Canon) on Jul 14, 2005 at 11:15 UTC
    Yes, you can:
    my @arr=map {$_, "=>$list{$_}"} keys %list;
    This should do what your code does, but your code doesn't seem to match exactly your description (and it has an unnecessary assignment)...
Re: use of 'map'
by artist (Parson) on Jul 14, 2005 at 11:17 UTC
    @arr = map { $_ => '=>'.$list{$_} } keys %list;
    --Artist
Re: use of 'map'
by radiantmatrix (Parson) on Jul 14, 2005 at 13:12 UTC

    Hm, you can use map{}, but I don't know if that's really the best way to do it, depending on your actual requirement. If all you need to do is add '=>' to the beginning of each value in your %list hash, you can do this:

    for (values %list) { $_ = '=>'.$_ }

    If you need to keep the old list, you can always make a copy and do the above to the copy only.

    If you really need to end up with an array of keys and values, you could do the above and then add one line:

    my @arr = %list;

    Just remember, while that will preserve key/value ordering, there is no guarantee about the order of the sets. In other words, a hash containing two pairs, 'one' => 1 and 'two' => 2 could result in two possible arrays: ( 'one',1,'two',2 ) or ( 'two',2,'one',1 ). The correct value will always follow the correct key, but that's about all you can guarantee (and that's always true when dealing with hashes).

    All that said, it looks like you might be trying to put a hash into a form that can be saved and then read back later. If that's your goal, then (depending on the particulars), you might forego all of this for Storable or Data::Dumper.

    <-radiant.matrix->
    Larry Wall is Yoda: there is no try{} (ok, except in Perl6; way to ruin a joke, Larry! ;P)
    The Code that can be seen is not the true Code
Re: use of 'map'
by polettix (Vicar) on Jul 14, 2005 at 13:06 UTC
    Today's the map day. See this post and answers to find pointers to be more "aware" of map usage.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.

Log In?
Username:
Password:

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

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

    No recent polls found