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

Re: Perl packages to convert hash refs to array refs and vice-versa?

by tirwhan (Abbot)
on Oct 31, 2005 at 09:39 UTC ( #504202=note: print w/replies, xml ) Need Help??


in reply to Perl packages to convert hash refs to array refs and vice-versa?

No module necessary, you can do

$array_ref=[%$hash_ref];
and
$hash_ref = {@$array_ref};

However, you want to be careful when doing that, for example when turning a hash into an array this way the array values end up in an unpredictable order(they'll always be key,value,key,value..., but the order of the keys will be unpredictable). When going the other way, your hash keys (i.e. the first,third,fifth... array element) need to be unique or you lose data. Without knowing what it is you're trying to achieve it's hard to give advice, so perhaps you should give a bit more detail on your goal with this?


Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it. -- Brian W. Kernighan

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2020-11-23 19:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?