Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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

by jesuashok (Curate)
on Oct 31, 2005 at 09:16 UTC ( #504196=perlquestion: print w/replies, xml ) Need Help??
jesuashok has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

Is there any perl module's available to convert :-

Hash reference into Array reference

Array reference into Hash Reference

"Keep pouring your ideas"

2005-10-31 Retitled by broquaint, as per Monastery guidelines
Original title: 'perl package's'

  • Comment on Perl packages to convert hash refs to array refs and vice-versa?

Replies are listed 'Best First'.
Re: Perl packages to convert hash refs to array refs and vice-versa?
by tirwhan (Abbot) on Oct 31, 2005 at 09:39 UTC

    No module necessary, you can do

    $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
Re: Perl packages to convert hash refs to array refs and vice-versa?
by dorward (Curate) on Oct 31, 2005 at 09:37 UTC

    Using a module for that strikes me as overkill...

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; # Create an array reference to demo this my $foo_ref = ['a', 'b', 'c', 'd']; # Convert array ref into hash ref and display my $bar_ref = {@{$foo_ref}}; print Dumper $bar_ref; # Convert hash ref into array ref and display my $new_foo_ref = [%{$bar_ref}]; print Dumper $new_foo_ref;

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://504196]
Approved by virtualsue
[marto]: your account is 7 days old, how old are these 'old tgimers'?
[choroba]: usemodpoerl Do you have a question? Do you want to help someone? Or do you just want to troll?
[usemodperl]: perlmonks used to be a safe space for perl coders, now it's like having an annoying girlfriend...
[marto]: I had a feeling I'd regret looking up the definition of 'soyboy'
[Veltro]: usemodperl Maybe it is the way you talk? 'Soy boys', 'old ladies' and what is even a tgimer...
usemodperl is venting, duh?
[usemodperl]: that was a typos, dumbass
[marto]: usemodperl I guess that depends on what you mean by a safe space, since many people seem to have the impression a safe space allows them to do/say whatever they feel like, without question or critque
[marto]: 'typos'->'typo'
[usemodperl]: it's like you guys are retarded or something, no sense of humor? autism?

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2018-06-24 15:43 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.