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

Returning multiple values from a subroutine

by rkrish (Acolyte)
on Dec 28, 2012 at 11:40 UTC ( #1010688=perlquestion: print w/ replies, xml ) Need Help??
rkrish has asked for the wisdom of the Perl Monks concerning the following question:

Hi,I'm trying to return an array and a variable from a subroutine,but I'm unable to get the desired output,please help me in correcting the code below

my ( @subs, $SubId ) = getsubsFromAcct( $currAcct ); my $length = @subs; print ("No of subs : $length and subs are @subs"); sub getsubsFromAcct { my $acctNum = shift; my @subs = (); my ( $sbscrpId, $count ); my $getSbscrp_id = $lda->prepare("select sbscrp from sbscrp_cltn w +here acct_nbr = ? "); my $subsOfSbscrp = $lda->prepare("select distinct subs_time from s +bscrp_time where sbscrp = ? "); $getSbscrp_id->bind_param(1,$acctNum); $getSbscrp_id->execute(); $getSbscrp_id->bind_col(1,\$sbscrpId); if ( $getSbscrp_id->fetch) { $subsOfSbscrp->bind_param(1,$sbscrpId); $subsOfSbscrp->execute() while ( my @subRow = $subsOfSbscrp->fetchrow_array() ) { push(@subs,@subRow); } } return ( \@subs, \$sbscrpId ); }
The output I got is :
No of subs : 2 and subs are ARRAY(0x2f8d10) SCALAR(0x2f8cf8)

Comment on Returning multiple values from a subroutine
Select or Download Code
Re: Returning multiple values from a subroutine
by vinoth.ree (Vicar) on Dec 28, 2012 at 11:49 UTC

    Hi, You are returning an Array reference and scalar reference, but the value get assigned into an array, So both the returning reference saved into @subs array

    .

    change the code as

    my ( $subs, $SubId ) = getsubsFromAcct( $currAcct ); my $length = @$subs; print ("No of subs : $length and subs are @$subs");

      Thanks for the quick reply :) it worked

        I'm glad it does. However, what I find more interesting is whether you understand why it works. References can be a tough subject to grasp. But you do, it's an immensely powerful tool that you should be glad to have in your toolbox. So are you just taking ree's solution as-is, happy it solves your problem, or do you see what he did and how that works?

Re: Returning multiple values from a subroutine
by McDarren (Abbot) on Dec 28, 2012 at 11:56 UTC

    You are returning a reference to an array, which is a scalar value.
    So you need to expect a scalar, and then de-reference it to get at the array values.

    Observe:
    #!/usr/bin/perl use strict; use warnings; my $currAcct = 1; my ( $subs, $SubId ) = getsubsFromAcct( $currAcct ); my $length = @$subs; print ("No of subs : $length and subs are @{$subs}"); sub getsubsFromAcct { my $acctNum = shift; my @subs = (); my $sbscrpId = 4; @subs = qw| 2 3 4 5 |; return ( \@subs, \$sbscrpId ); }
    Prints:
    No of subs : 4 and subs are 2 3 4 5

    Hope this helps
    Darren

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (13)
As of 2014-04-18 16:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (470 votes), past polls