Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

mapping two arrays into anon hash

by agoth (Chaplain)
on Jan 08, 2002 at 15:25 UTC ( [id://137095]=perlquestion: print w/replies, xml ) Need Help??

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

Hola, I am trying to map two fixed and known lenght lists into a hash, preferably without using variables.

I want to replace myfunc below with a short but legible bit of code, the last attempt below ends up using $_, @_ and %_ which is a bit mean.

Any better offers??

my @usual = qw ( data email stuff ); #--- fixed length my $i = 0; my %hash; my @input = ( #--- rather have no function { username => 'fred1', myfunc ( 12, 56, 78 ) }, #--- no use if have to reset $i { username => 'fnerk', map { $usual[ $i++ ] => $_ } (33, 44, 55) } +, #--- hmm { username => 'aargh', map { @hash{@usual} = @$_; %hash } [ 33, 44 +, 55 ] }, #--- longwinded { username => 'fnerk', map { $tmp = shift @usual; push @usual,$tmp; $tmp, $_ } (33, 44, 55) }, #--- heinous { username => 'aargh', map { @_{ @usual } = @$_; %_ } [ 33, 44, 55 + ] }, ); sub myfunc { my %flib; @flib{ @usual } = @_; return %flib; } for ( @input ) { my %local = %$_; for (sort keys %local ) { print "key : $_ : val : $local{$_} : "; } print "\n"; }

Replies are listed 'Best First'.
Re: mapping two arrays into anon hash
by Masem (Monsignor) on Jan 08, 2002 at 17:07 UTC
    Use Hash Slices:
    use Data::Dumper; my @usual = qw( data email stuff ); my @values = qw( 33 44 55 ); my %hash; @hash{ @usual } = @values; print Dumper \%hash;

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

      I know about hash slices !!, and am using one in the sub...
      I'm trying to slice anonymously without defining the hash explicitly, in order to build up a data structure with the minimum amount of effort..
      looking for { username => 'fred' , @{}{@usual} fancyslice @values };
      which led me to the @_{@ary1}; %_ monstrosity...
        You're asking for the impossible, or if it is, it's going to be an ugly mess. Sure, it's nice to get a simple result that doesn't use extra variables, but if it becomes unreadible, then you're going to confuse others as to the meaning of your code. In this case, using a temporary hash is not going to increase your memory usage save for maybe an extra byte if you create a reference to the hash, and the code will be much easier to read.

        But here's another solution that doesn't use any extra variables beyond $_:

        use Data::Dumper; my @names = qw ( web email stuff ); my @values = qw( 33 44 55 ); my $ref = { map { $names[$_] => $values[$_] } (0..$#names) }; print Dumper $ref;
        (It's still not as clean as using the hash slice, IMO, but if that's what you want...)

        -----------------------------------------------------
        Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
        "I can see my house from here!"
        It's not what you know, but knowing how to find it if you don't know that's important

Re (tilly) 1: mapping two arrays into anon hash
by tilly (Archbishop) on Jan 08, 2002 at 19:24 UTC
Re: mapping two arrays into anon hash
by jmcnamara (Monsignor) on Jan 08, 2002 at 17:22 UTC

    You could use an array slice:     { username => 'slice', ( @usual, 33, 44, 55 )[0, 3, 1, 4, 2, 5] },I don't know if you could classify that as legible however.

    Personally, I'd prefer the subroutine approach for maintainability.

    --
    John.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2024-04-19 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found