Sub returning a scalar and an array ref?

Sub returning a scalar and an array ref?

am 01.08.2007 20:20:48 von Deane.Rothenmaier

This is a multipart message in MIME format.
--===============1070646985==
Content-Type: multipart/alternative;
boundary="=_alternative 0064D6E58625732A_="

This is a multipart message in MIME format.
--=_alternative 0064D6E58625732A_=
Content-Type: text/plain; charset="us-ascii"

Gurus,

Is it possible for a subroutine to return both a scalar (an integer,
specifically) *and* a reference to an array of arrays?

Current code returns just the array ref, with the scalar push-ed onto the
end of the referenced array. I'm hoping to get rid of the kludge, and I've
not found much help in the camel (LITWP, maybe?). Here's sample code:

# sub adds a row to the passed array (of arrays) and returns it, along
with incremented counter
sub add_row {
my ($counter, @ary) = @_;

# do stuff to @ary...

# add scalar to @ary:
push( @ary, ++$counter );

return( \@ary );
}

# sample call:
@added_stuff = @{ add_row( $out_count, @added_stuff) };
$out_count = pop( @added_stuff );

I know there's gotta be a cleaner way to do this, but my brain's frozen on
this one.

TIA!

Deane Rothenmaier
Programmer/Analyst
Walgreens Corp.
847-914-5150

Adversity is the trial of principle. Without it a man hardly knows whether
he is honest or not. -- Henry Fielding
--=_alternative 0064D6E58625732A_=
Content-Type: text/html; charset="us-ascii"



Gurus,



Is it possible for a subroutine to return both a scalar (an integer, specifically) *and* a reference to an array of arrays?



Current code returns just the array ref, with the scalar push-ed onto the end of the referenced array. I'm hoping to get rid of the kludge, and I've not found much help in the camel (LITWP, maybe?).  Here's sample code:



# sub adds a row to the passed array (of arrays) and returns it, along with incremented counter

sub add_row {

my ($counter, @ary) = @_;



   # do stuff to @ary...



   # add scalar to @ary:

   push( @ary, ++$counter );



   return( \@ary );

}



# sample call:

@added_stuff = @{ add_row( $out_count, @added_stuff) };

$out_count = pop( @added_stuff );



I know there's gotta be a cleaner way to do this, but my brain's frozen on this one.



TIA!



Deane Rothenmaier

Programmer/Analyst

Walgreens Corp.

847-914-5150



Adversity is the trial of principle. Without it a man hardly knows whether he is honest or not. -- Henry Fielding

--=_alternative 0064D6E58625732A_=--


--===============1070646985==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
--===============1070646985==--

Re: Sub returning a scalar and an array ref?

am 01.08.2007 21:14:46 von Todd Beverly

Deane.Rothenmaier@walgreens.com wrote:
>
> Gurus,
>
> Is it possible for a subroutine to return both a scalar (an integer,
> specifically) *and* a reference to an array of arrays?
>
> Current code returns just the array ref, with the scalar push-ed onto
> the end of the referenced array. I'm hoping to get rid of the kludge,
> and I've not found much help in the camel (LITWP, maybe?). Here's
> sample code:
>
> # sub adds a row to the passed array (of arrays) and returns it, along
> with incremented counter
> sub add_row {
> my ($counter, @ary) = @_;
>
> # do stuff to @ary...
>
> # add scalar to @ary:
> push( @ary, ++$counter );
>
> return( \@ary );
> }
>
> # sample call:
> @added_stuff = @{ add_row( $out_count, @added_stuff) };
> $out_count = pop( @added_stuff );
>
> I know there's gotta be a cleaner way to do this, but my brain's
> frozen on this one.
>
You could do something like this:
#!/usr/bin/perl -w
use strict;

my @ary = qw(1 2 3);
my $oldcount = 5;

my ($count, $ref_ary) = add_row($oldcount, @ary);
@ary = @{$ref_ary};

print "ary is now: ", join(', ', @ary), " count is $count\n";

sub add_row {
my ($counter, @ary) = @_;

# do stuff to @ary...

# add scalar to @ary:
push( @ary, ++$counter );

return($counter, \@ary );
}

Or this, if you don't have to return a reference to an array.
#!/usr/bin/perl -w
use strict;

my @ary = qw(1 2 3);
my $oldcount = 5;

my $count;
($count, @ary) = add_row($oldcount, @ary);

print "ary is now: ", join(', ', @ary), " count is $count\n";

sub add_row {
my ($counter, @ary) = @_;

# do stuff to @ary...

# add scalar to @ary:
push( @ary, ++$counter );

return($counter, @ary );
}

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Re: Sub returning a scalar and an array ref?

am 02.08.2007 22:39:36 von Andy_Bach

> Is it possible for a subroutine to return both a scalar (an integer,
specifically) *and* a reference to an array of arrays?
sub add_row {
my ($counter, @ary) = @_;

# do stuff to @ary...

# add scalar to @ary:
push( @ary, ++$counter );

return( \@ary );
}

# sample call:
@added_stuff = @{ add_row( $out_count, @added_stuff) };
$out_count = pop( @added_stuff );

yeah, you can return a list in list context.
sub add_row {
my ($counter, @ary) = @_;
# do stuff to @ary...
++$counter;

return( \@ary, $counter);
}

my ($add_stuff_ref, $out_count) = add_row($out_count, @added_stuff);

A little counter-intuitive, the call and return orderings here. So this
does smack of some need for refactoring here. You could pass these by
ref, not value, so that would avoid some of this sort of back and forth.
sub add_row {
my ($counter_ref, $ary_ref) = @_;
# do stuff to @{ $ary_ref }...
++$counter_ref;
return;
}

add_row( \$out_count, \@added_stuff);

That's ugly action at a distance so use the return value as some kind of
success marker:
sub add_row {
my ($ary_ref) = @_;
my $do_stuff_worked = 0;
# do stuff to @{ $ary_ref }...
return $do_stuff_worked;
}

$out_count++ if add_row(\@added_stuff);

unless you need $out_count in the "do stuff" code. But it's a start.

a

Andy Bach
Systems Mangler
Internet: andy_bach@wiwb.uscourts.gov
VOICE: (608) 261-5738 FAX 264-5932

"Men are always wicked at bottom unless they are made good by some
compulsion."
Niccolo Macchiavelli
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs