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

Re: use of 'map'

by radiantmatrix (Parson)
on Jul 14, 2005 at 13:12 UTC ( [id://474858]=note: print w/replies, xml ) Need Help??


in reply to use of 'map'

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-03-29 09:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found