Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Returning values from a sub routine.

by Anonymous Monk
on Jul 31, 2012 at 14:56 UTC ( [id://984604]=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 a similar problem as I saw it here and using the same sample code my question is, what if some of these elements are repeated, how would be the best way to return these values from the sub routine, would I have to use a two dimensional array instead?
use strict; use warnings; my $info = test(); for my $name (sort keys %$info) { #print "$name: $info->{$name}\n"; my $checking = printing( names => $name, emails => $info->{$name}, ); } sub printing { my (%args) = @_; my $names = $args{names} || ''; my $emails = $args{emails} || ''; # need to count how many unique and repeated items and print results print "Unique=$names: $emails\n"; print "Dups =$names: $emails\n"; } sub test { my (@names_all, @email_all); my @names = qw(Joe mary ann pete amy jerry Joe ann John John ); 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 John@test.com John@ok.com); my %emails; @emails{@names_all} = @email_all; @emails{@names} = @email; return \%emails; }

Thanks for the help!

Replies are listed 'Best First'.
Re: Returning values from a sub routine.
by suaveant (Parson) on Jul 31, 2012 at 15:02 UTC
    Either use an anonymous array, or store the values in the hash as arrays so that you can have multiple emails per name. Then you'd do something like
    for my $email (@{$info->{$name}}) { my $checking = printing( names => $name, emails => $email, ); }
    Or you could possibly just combine emails into a comma separated list if that is an acceptable output.

                    - Ant
                    - Some of my best work - (1 2 3)

      Would you be able to post some sample code using the code posted here using an anonymous array to return the values?
        I can get you started.

        You wouldn't be able to assign directly to a hash slice like you do, you'd have to iterate. If using a hash of arrays I'd always use an array as the value, even if its only one value, will help keep the code simpler. You can loop through both arrays separately or combine them

        for my $name ( @names_combined ) { push @{$emails{$name}}, shift(@emails_combined); }

                        - Ant
                        - Some of my best work - (1 2 3)

Re: Returning values from a sub routine.
by 2teez (Vicar) on Jul 31, 2012 at 20:27 UTC
    Hi,

    Please, try the code below it's an addition to suaveant previous code.
    It should do what you want or you can improve on it, until you get what you really want!

    use strict; use warnings; my $info = test(); printing($info); sub printing { my ($args) = @_; foreach my $name ( keys %{$args} ) { # tell the number of emails attached to a person my $count = scalar @{ $args->{$name} }; # if more than one email is attached to a person # that individual is not Unique $count == 1 ? print "Unique: ", $name, " email: ", @{ $args->{$name} }, +$/ : print "Dups: ", $name, map { " email: $_$/" } @{ $args->{ +$name} }; } } sub test { my @names = qw(Joe mary ann pete amy jerry Joe ann John John ); 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.co +m John@test.com John@ok.com); my %emails; foreach my $name (@names) { push @{ $emails{$name} }, shift @email; } return \%emails; }
    Output
    Unique: jerry email: jerry@b.com Unique: pete email: pete@here.com Dups: Joe email: joe@test.com email: joe@test.com Dups: John email: John@test.com email: John@ok.com Unique: amy email: amy@ok.com Dups: ann email: ann@nowhere.com email: ann@nowhere.com Unique: mary email: mary@test.com
    You might want to check the following documentation for more info:
    • perldsc
    • perldata
    • perllol
    Also check this module: Data::Dumper
    it stringified perl data structures, suitable for both printing and eval!!!

Log In?
Username:
Password:

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

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

    No recent polls found