Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
go ahead... be a heretic
 
PerlMonks  

looking for a perl way to build a hash

by Anonymous Monk
on Jul 26, 2013 at 13:46 UTC ( #1046512=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have 2 strings and I want to create a hash with the pairwise elements
my $str1="a,b,c" my $str2="1,2,3" ## output: { a => 1, b => 2, c => 3}
I have been trying to come up with a nice concise perlish way of building this hash using map, split and pop/shift (etc) rather than the boring old for loop with indices. It seems so simple, but yet my perl is just not cutting it at the moment. Anyone have any cool solutions ? Best Regards, Michael

Comment on looking for a perl way to build a hash
Download Code
Re: looking for a perl way to build a hash
by Anonymous Monk on Jul 26, 2013 at 13:53 UTC
Re: looking for a perl way to build a hash
by AnomalousMonk (Monsignor) on Jul 26, 2013 at 13:53 UTC
    >perl -wMstrict -le "my $str1 = 'a,b,c'; my $str2 = '1,2,3'; ;; my %hash; @hash{ split q{,}, $str1 } = split q{,}, $str2; ;; use Data::Dump; dd \%hash; " { a => 1, b => 2, c => 3 }
Re: looking for a perl way to build a hash
by BrowserUk (Pope) on Jul 26, 2013 at 13:55 UTC

    $str1="a,b,c";; $str2="1,2,3";; @hash{ split ',',$str1 } = split ',', $str2;; pp \%hash;; { a => 1, b => 2, c => 3 }

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: looking for a perl way to build a hash
by toolic (Chancellor) on Jul 26, 2013 at 13:55 UTC
    List::MoreUtils
    use warnings; use strict; use List::MoreUtils qw(mesh); my $str1="a,b,c"; my $str2="1,2,3"; my @a1 = split /,/, $str1; my @a2 = split /,/, $str2; my %hash = mesh(@a1, @a2); use Data::Dumper; $Data::Dumper::Sortkeys=1; print Dumper(\%hash); __END__ $VAR1 = { 'a' => '1', 'b' => '2', 'c' => '3' };
Re: looking for a perl way to build a hash
by hdb (Parson) on Jul 26, 2013 at 14:06 UTC

    use strict; use warnings; use Data::Dumper; use List::MoreUtils qw(mesh); my $str1="a,b,c"; my $str2="1,2,3"; my %hash = mesh @{[ split/,/,$str1 ]}, @{[ split/,/,$str2 ]}; print Dumper \%hash;
Re: looking for a perl way to build a hash
by choroba (Abbot) on Jul 26, 2013 at 14:11 UTC
    TIMTOWTDI:
    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $str1 = 'a,b,c'; my $str2 = '1,2,3'; my ($strA, $strB) = ($str1, $str2); $strA =~ s/,/',' . do { $strB =~ s=([^,]+),==; $1 } . ','/eg; my %hash2 = split /,/ => "$strA,$strB"; print Dumper \%hash2;

    Update: Or, without a regex:

    my ($strA, $strB) = ("$str1,", "$str2,"); my %hash = map { map substr($_, 0, -1), map substr($_, 0, 1 + index($_, ','), q()), $strA, $strB; } 1 .. $strA =~ tr/,//; print Dumper \%hash;

    Not recommended for production code.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: looking for a perl way to build a hash
by code-ninja (Scribe) on Jul 26, 2013 at 19:07 UTC
    Everyone has put in their ways, I'll put mine too for consideration... :)
    #!/usr/bin/perl use strict; use warnings; my $str1 = "1,2,3"; my $str2 = "a,b,c"; my ($k, %hash, $i); $i = 0; my @keys = split /,/, $str1; my @values = split /,/, $str2; foreach $k (@keys){ $hash{$k} = $values[$i++]; } print %hash;

    I leave it up to you to print the hash in the formatted way

Re: looking for a perl way to build a hash
by brx (Pilgrim) on Jul 29, 2013 at 17:50 UTC
    Dirty golfer
    my $str1="a,b,c"; my $str2="1,2,3"; @h{eval$str1}=eval$str2
    Don't do that, see other replies!
    English is not my mother tongue.
    Les tongues de ma mère sont "made in France".

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (15)
As of 2014-04-16 16:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (432 votes), past polls