Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

how to split hash into two arrays

by baxy77bax (Deacon)
on Jul 07, 2009 at 07:49 UTC ( [id://777777]=perlquestion: print w/replies, xml ) Need Help??

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

hi,

i need a quick oneliner for this script if one exists:

my $array->[0] = $arg{key1}; my $key->[0] = 'key1' if ($array->[0]);
so basically i have a hash that needs to be broken into two arrays and depending on a hash key the position of the key and value needs to be placed on a specific position in two corresponding arrays

thank you

Update:

ok i have a sub() that takes some variables. and the way to pass some variables to that sub is through hash interface.like this:

sub _sub { my %arg = @_; my $array; # sometimes is treated as a scalar $array->[0] = $arg{key1}; $array->[1] = $arg{key2}; $array->[2] = $arg{key3}; $array->[3] = $arg{key4}; ... }
sometimes user can pass only one variable to the _sub(). for example
_sub(key3 => 'big-mama');
in this case only the $array->[2] will be defined and all others will be undef. so what i need is one extra array that will tell me the key (key3) that corresponds to that variable and that key should be placed on a $secarray->[2] position

Replies are listed 'Best First'.
Re: how to split hash into two arrays
by Your Mother (Archbishop) on Jul 07, 2009 at 08:00 UTC

    Possibly (sorry, not one liner)-

    use YAML; my ( @one, @two ); my %one_two = map {; $_ => int(rand(2) } 1 .. 10; for my $key ( keys %one_two ) { push @one, $key; push @two, $one_two{$key} || undef; # "skips" 0s here. } print Dump ( \%one_two, \@one, \@two );
Re: how to split hash into two arrays
by missingthepoint (Friar) on Jul 07, 2009 at 08:04 UTC

    Your post is a little dense and also slightly confusing. This is the best I can do, but I'd like you to clarify what you want. In particular, what do you mean by depending on a hash key the position of the key and value needs to be placed on a specific position in two corresponding arrays?

    # assume %h is your hash, @c and @d are the 'corresponding arrays' @keys=keys %h;@vals=values %h;for($i=0;$i<@keys;$i++){$c[9] = $d[9] = +$i if $keys[$i] eq 'something'}

    I'm assuming:

    • By 'depending on a hash key' you mean 'if one particular key exists in the hash'
    • You know the 'specific position' (ie offset) in the other two arrays in which you want to place the offset of the particular key/value (in the code I've used 9 arbitrarily)

    The zeroeth step in writing a module is to make sure that there isn't already a decent one in CPAN. (-- Pod::Simple::Subclassing)
Re: how to split hash into two arrays
by Anonymous Monk on Jul 07, 2009 at 08:00 UTC
    i need a quick oneliner for this script if one exists Happy?
    my $array->[0] = $arg{key1};my $key->[0] = 'key1' if ($array->[0]);
    :D
Re: how to split hash into two arrays
by grizzley (Chaplain) on Jul 07, 2009 at 11:42 UTC
    I think you don't need any extra array. Just the sub you already have. If some param is not passed, null will be assigned to $array->[x]:
    C:\>perl -le "use strict;use warnings;sub f{my($x,$y)=@_;my $aa=$x;my +$bb=$y; print $aa, ' ', $bb}; f(1); f(2,3)" Use of uninitialized value $bb in print at -e line 1. 1 2 3
    This means, that in first case $bb equals 'undef' as you want.

Log In?
Username:
Password:

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

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

    No recent polls found