Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Returning a hash instead of two arrays help!

by Anonymous Monk
on Jul 30, 2012 at 19:28 UTC ( [id://984497]=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 would like to make and return a hash instead of returning two arrays, using the sample code below to illustrate my issue how can I do this, and how would I print this hash like I am printing it using the array in my sample code?
... sub test { my (@names_all, @email_all); my @name = qw(Joe mary ann pete amy jerry); my @email = qw(joe@test.com mary@test.com ann@nowhere.com pete@here.co +m amy@ok.com jerry@b.com); foreach $names(@names) { push @name_all, $names; } foreach $emails(@email) { push @email_all, $emails; } # need to make a hash instead of sending two arrays, how?? #return \@name_all, \@email; return \@name_all; } my $info = test(); foreach my $all(@$info) { print "\n$all\n"; } ...

Thanks a lot!

Replies are listed 'Best First'.
Re: Returning a hash instead of two arrays help!
by davido (Cardinal) on Jul 30, 2012 at 19:52 UTC

    No intermediate hash necessary:

    use List::MoreUtils qw( zip ); sub test { my @name = qw( Joe mary ann pete amy jerry ); my @email = qw( joe@test.com mary@test.com ann@nowhere.com pete@he +re.com amy@ok.com jerry@b.com ); return zip @name, @email; }

    See List::MoreUtils (mesh or its alias zip). This is also built into Perl6 as (I think), the Z operator.


    Dave

Re: Returning a hash instead of two arrays help!
by toolic (Bishop) on Jul 30, 2012 at 19:41 UTC
    use warnings; use strict; sub test { my %email_of; my @name = qw(Joe mary ann pete amy jerry); my @email = qw(joe@test.com mary@test.com ann@nowhere.com pete@her +e.com amy@ok.com jerry@b.com); for my $i (0 .. $#name) { $email_of{$name[$i]} = $email[$i]; } return \%email_of; } my $info = test(); foreach my $all (%$info) { print "$all\n"; }

    Even fancier (see perldata):

    sub test { my @name = qw(Joe mary ann pete amy jerry); my @email = qw(joe@test.com mary@test.com ann@nowhere.com pete@her +e.com amy@ok.com jerry@b.com); my %email_of; @email_of{@name} = @email; return \%email_of; }
Re: Returning a hash instead of two arrays help!
by GrandFather (Saint) on Jul 30, 2012 at 20:01 UTC

    There are lots of ways this could go and a best answer depends a lot on the bigger picture. In the context of the narrow picture shown using a hash slice or two is the easy way to populate a hash:

    use strict; use warnings; my $info = test(); for my $name (sort keys %$info) { printf "%-10s %s\n", "$name:", $info->{$name}; } sub test { my (@names_all, @email_all); my @names = qw(Joe mary ann pete amy jerry); my @email = qw(joe@test.com mary@test.com ann@nowhere.com pete@here.com amy@ok.com jerry@b.com); my %emails; @emails{@names_all} = @email_all; @emails{@names} = @email; return \%emails; }

    Prints:

    Joe: joe@test.com amy: amy@ok.com ann: ann@nowhere.com jerry: jerry@b.com mary: mary@test.com pete: pete@here.com
    True laziness is hard work
      Hi, I hope someone will get to this still, I have a similar problem and my question is, what if some of these elements are repeated what would be the best way to return these values?
      use strict; use warnings; my $info = test(); for my $name (sort keys %$info) { printf "%-10s %s\n", "$name:", $info->{$name}; } sub test { my (@names_all, @email_all); my @names = qw(Joe mary ann pete amy jerry Joe ann); my @email = qw(joe@test.com mary@test.com ann@nowhere.com pete@here.com amy@ok.com jerry@b.com joe@test.com ann@nowhere. +com); my %emails; @emails{@names_all} = @email_all; @emails{@names} = @email; return \%emails; }

      Thanks!

        It depends on why you have "repeated elements". If you have multiple people with the same name then you probably need to use an ID code as the hash key instead of the name. If you have multiple email addresses per person then the value becomes an array instead of a single value and you can thus track all the addresses.

        True laziness is hard work
Re: Returning a hash instead of two arrays help!
by linuxkid (Sexton) on Jul 31, 2012 at 19:57 UTC

    remember a hash can be converted to an array, and vice versa, as long as it is in the order of Key, Value, Key, Value, you can return your array and get the return value as a hash.

    --linuxkid


    imrunningoutofideas.co.cc

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-29 07:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found