[comp.lang.perl] Returning two lists

brossard@sic.epfl.ch (Alain Brossard EPFL-SIC/SII) (04/03/91)

   I want to return two lists from a subroutine (which is
recursive).  (I'm using perl 4.000).

   My code is the following:
sub search {
    local( $dir, $start, @list, @files, $file, @mem_list,
		    @whiteouts, @rtn_list, @rtn_wo ) = @_;
(...)
    return ( @list, @whiteouts );
# also tried
#   return ( (@list), (@whiteouts) );
}

(@list, @whiteouts) = do search( $dir, '' );

	The only thing I get is that on return, @list
always contains the union of the returned lists.

	Any suggestions?


				Alain Brossard

me@anywhere.EBay.Sun.COM (Wayne Thompson - IR Workstation Support SE) (04/03/91)

--- Alain Brossard writes ---
   I want to return two lists from a subroutine (which is
recursive).  (I'm using perl 4.000).

   My code is the following:
sub search {
    local( $dir, $start, @list, @files, $file, @mem_list,
		    @whiteouts, @rtn_list, @rtn_wo ) = @_;
(...)
    return ( @list, @whiteouts );
# also tried
#   return ( (@list), (@whiteouts) );
}

(@list, @whiteouts) = do search( $dir, '' );

	The only thing I get is that on return, @list
always contains the union of the returned lists.

	Any suggestions?

				Alain Brossard
-----------------------------

You could return enough information to decompose the lists...

return (scalar(@list), @list, @whiteouts);

Then recompose them on receipt...

@_ &search(....);
@whiteouts = splice (@_, shift (@_));
@list = @_;

Wayne

worley@compass.com (Dale Worley) (04/04/91)

   From: brossard@sic.epfl.ch (Alain Brossard EPFL-SIC/SII)

       return ( @list, @whiteouts );

Since Perl has no arrays of arrays, the (...) operator (in an array
context) concatenates any arrays that it is given.

Dale

Dale Worley		Compass, Inc.			worley@compass.com
--
Cthulhu -- when you are tired of choosing the lesser of two evils.

brossard@sic.epfl.ch (Alain Brossard EPFL-SIC/SII) (04/06/91)

In article <6010@male.EBay.Sun.COM>, me@anywhere.EBay.Sun.COM (Wayne Thompson - IR Workstation Support SE) writes:
|> --- Alain Brossard writes ---
|>    I want to return two lists from a subroutine
|> 
|> sub search {
|>     local( @list, @whiteouts ) = @_;
|> (...)
|>     return ( @list, @whiteouts );
|> # also tried
|> #   return ( (@list), (@whiteouts) );
|> }
|> 
|> (@list, @whiteouts) = do search( $dir, '' );
|> 
|> 	The only thing I get is that on return, @list
|> always contains the union of the returned lists.
|> -----------------------------
> 
> You could return enough information to decompose the lists...
> 
> return (scalar(@list), @list, @whiteouts);

  Will scalar(ARRAY) always return the size of the array?
I didn't see that documented anywhere.

> Then recompose them on receipt...
> 
> @_ &search(....);
> @whiteouts = splice (@_, shift (@_));
> @list = @_;
> 
> Wayne

  Thanks for pointing me out the splice fonction. I think
the following code is a bit more efficient?

@whiteouts = do search( $dir, '' );
@list = splice( @whiteouts, 0, shift(@whiteouts));

    @list can be potentialy huge, so I was really looking
for the optimal way of doing this.  The best way would be to
(shudder) use global variables, but this is out since the
subroutine is recursive (and not tail-end recursive).


-- 

Alain Brossard, Ecole Polytechnique Federale de Lausanne,
	SIC/SII, EL-Ecublens, CH-1015 Lausanne, Suisse
brossard@sasun1.epfl.ch

me@anywhere.EBay.Sun.COM (Wayne Thompson - IR Workstation Support SE) (04/06/91)

From pg 73 of the book "...the value of an array in a scalar
context is the length of the array"

Yes, your's is more efficient. Hope that helps.

Wayne

In article <1991Apr5.182305@sic.epfl.ch>, brossard@sic.epfl.ch (Alain Brossard EPFL-SIC/SII) writes:
| In article <6010@male.EBay.Sun.COM>, me@anywhere.EBay.Sun.COM (Wayne Thompson - IR Workstation Support SE) writes:
| || --- Alain Brossard writes ---
| ||    I want to return two lists from a subroutine
| || 
| || sub search {
| ||     local( @list, @whiteouts ) = @_;
| || (...)
| ||     return ( @list, @whiteouts );
| || # also tried
| || #   return ( (@list), (@whiteouts) );
| || }
| || 
| || (@list, @whiteouts) = do search( $dir, '' );
| || 
| || 	The only thing I get is that on return, @list
| || always contains the union of the returned lists.
| || -----------------------------
| | 
| | You could return enough information to decompose the lists...
| | 
| | return (scalar(@list), @list, @whiteouts);
| 
|   Will scalar(ARRAY) always return the size of the array?
| I didn't see that documented anywhere.
| 
| | Then recompose them on receipt...
| | 
| | @_ = &search(....);
| | @whiteouts = splice (@_, shift (@_));
| | @list = @_;
| | 
| | Wayne
| 
|   Thanks for pointing me out the splice fonction. I think
| the following code is a bit more efficient?
| 
| @whiteouts = do search( $dir, '' );
| @list = splice( @whiteouts, 0, shift(@whiteouts));
| 
|     @list can be potentialy huge, so I was really looking
| for the optimal way of doing this.  The best way would be to
| (shudder) use global variables, but this is out since the
| subroutine is recursive (and not tail-end recursive).
| 
| 
| -- 
| 
| Alain Brossard, Ecole Polytechnique Federale de Lausanne,
| 	SIC/SII, EL-Ecublens, CH-1015 Lausanne, Suisse
| brossard@sasun1.epfl.ch

allbery@NCoast.ORG (Brandon S. Allbery KB8JRR/AA) (04/08/91)

As quoted from <1991Apr3.233558.11641@uvaarpa.Virginia.EDU> by worley@compass.com (Dale Worley):
+---------------
|    From: brossard@sic.epfl.ch (Alain Brossard EPFL-SIC/SII)
| 
|        return ( @list, @whiteouts );
| 
| Since Perl has no arrays of arrays, the (...) operator (in an array
| context) concatenates any arrays that it is given.
+---------------

Well, there's always:

	$gensym00000000 = 'gensym00000000';
	sub gensym { ++$gensym000000000; }

	sub foo
	{
		local(*X, *Y) = (&gensym, &gensym)

		... /* use @X and @Y */
		(*X, *Y);
	}

	... /* main program */
	undef @arr1;
	undef @arr2;
	(*arr1, *arr2) = &foo(...);

(But if you don't understand it, you almost certainly don't want anything to
do with it:  it's major black magic.)

++Brandon
-- 
Me: Brandon S. Allbery			  Ham: KB8JRR/AA on 2m, 220, 440, 1200
Internet: allbery@NCoast.ORG		(QRT on HF until local problems fixed)
America OnLine: KB8JRR // Delphi: ALLBERY   AMPR: kb8jrr.AmPR.ORG [44.70.4.88]
uunet!usenet.ins.cwru.edu!ncoast!allbery          KB8JRR @ WA8BXN.OH