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

return more than 1 value

by changma_ha (Sexton)
on Jul 29, 2010 at 06:29 UTC ( #851852=perlquestion: print w/replies, xml ) Need Help??

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

I have a doubt regarding the return value. suppose i have a perl module in which i have define a method for hash. suppose i have a package mymodule.pm in which i have a method like

sub ahash{ my %hash=@_; foreach my $key(keys %hash){ my @ke = keys %hash; my @val = values %hash; return @ke; return @val; } }

And i am calling it like

my @list =qw (1 2 3 4 5); print addmodule::ahash(@list),"\n";

Now my question is can i retun more than 1 value when i defining the mothod ?like i have done in return @ke and return @val...

Replies are listed 'Best First'.
Re: return more than 1 value
by roboticus (Chancellor) on Jul 29, 2010 at 06:43 UTC

    changma_ha:

    Like so:

    Roboticus@Roboticus-PC ~ $ cat ret.pl #!/usr/bin/perl my ($x, $y) = foo(); print "x=$x, y=$y\n"; sub foo { return 3, 5 } Roboticus@Roboticus-PC ~ $ perl ret.pl x=3, y=5

    You simply return a list of values. You can't use two successive return statements, because after the first one, the second will never execute.

    ...roboticus

Re: return more than 1 value
by Ratazong (Monsignor) on Jul 29, 2010 at 06:43 UTC

    The subroutine ends with the return-statement. Therefore, in your case the line  return @val; will never be executed.

    In order to return more than one value, put all your return-values in a list, array or a hash and return that. The caller of your sub will then need to split the individual return-values ...

    HTH, Rata
Re: return more than 1 value
by Utilitarian (Vicar) on Jul 29, 2010 at 08:38 UTC
    Hi changma_ha,

    There are a few issues in your code, as pointed out above you can only return from a subroutine once so the second return is meaningless.

    However if you return two arrays they are "flattened" and become indistinguishable. Take a look at the return documentation

    As a result you should return references to the two arrays (or other data structure)

    I have provided you with a code sample with comments which should make things clearer, play about with it and feel free to ask if you don't follow what it is doing.

    use strict; use warnings; my @list =qw (1 2 3 4 5); # define a hash to take back the values my %returned_values; # get the array references my ($keys, $values) = apackage::ahash(@list); # Put them in a hash @returned_values{@{$keys}} = @{$values}; # print out for my $key (@{$keys}){ print "$key maps to $returned_values{$key} \n"; } package apackage; sub ahash{ my %hash; # You are calling the subroutine with an uneven number of elements, # You should check for this and correct before assigning to a hash if ((@_ % 2) != 0){ push (@_,"null"); } %hash = @_ ; # Your foreach loop made no sense here my @ke = keys %hash; my @val = values %hash; # return references to your two arrays return (\@ke, \@val); # in this context returning a hash makes more sense }
    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

      Thank you a lots Utilitarian....your example made my doubt clear..i will ask you..if i have any problems.

      @ Utilitarian,i have another question regarding a line of your code. in @returned_values{@{$keys}} = @{$values}; how did u take a %returned_values as an array? i mean i am not getting this line properly. Thanks in advance

        @hash{@array_of_keys}=@array_of_values;
        Is the Perlish way of assigning an array of values to their equivalent keys. You could, (if you enjoy typing), write
        for my $index (0..$#array_of_keys){ $hash{$array_of_keys[$index]}=$array_of_values[$index]; }
        In the line you cite there isn't an array @returned_values it is a hash slice. take a look at the perldata man page for further, better presented, info

        print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://851852]
Approved by tye
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: (3)
As of 2022-08-10 23:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?