Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Keep It Simple, Stupid

Returning values from a sub routine.

by Anonymous Monk
on Jul 31, 2012 at 14:56 UTC ( #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( ann@nowhere. +com; my %emails; @emails{@names_all} = @email_all; @emails{@names} = @email; return \%emails; }

Thanks for the help!

Comment on Returning values from a sub routine.
Download Code
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 (Priest) on Jul 31, 2012 at 20:27 UTC

    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( +m; my %emails; foreach my $name (@names) { push @{ $emails{$name} }, shift @email; } return \%emails; }
    Unique: jerry email: Unique: pete email: Dups: Joe email: email: Dups: John email: email: Unique: amy email: Dups: ann email: email: Unique: mary email:
    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?

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2014-04-20 12:37 GMT
Find Nodes?
    Voting Booth?

    April first is:

    Results (485 votes), past polls