Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: use of 'map'

by radiantmatrix (Parson)
on Jul 14, 2005 at 13:12 UTC ( #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.

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?

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2020-06-04 18:47 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (35 votes). Check out past polls.