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

hi , monks!
i try to build up my first class :) but i get a problem with returning result
in the class ~
###in textgrab.pm , 3 array lists contains different data sub GetZone{ . . . return @Zone, @reverseZone, @reverseZoneShow; } ### in main.pl ... my $obj=new textgrab; (@Zone, @RevZone, @RevZoneShow)=$obj->GetZone();
mm , but the results shows @Zone obtains all the array list(i.e. @Zone contains all data of @Zone, @RevZone, @RevZoneShow ,
but @RevZone, @RevZoneShow contains nothing) , what can i do?? thx! :|

Replies are listed 'Best First'.
Re: problem : return an array list
by Kanji (Parson) on Mar 31, 2002 at 04:52 UTC

    To differentiate between arrays when passing to / receiving from a sub, etc. you need to use array references so that the first array doesn't gobble up the rest.

    ###in textgrab.pm , 3 array lists contains different data sub GetZone{ . . . return \@Zone, \@reverseZone, \@reverseZoneShow; } ### in main.pl ... my $obj=new textgrab; ($Zone, $RevZone, $RevZoneShow)=$obj->GetZone(); # Can now accessed as @$Zone, etc., or manually # reassign them with my @Zone = @$Zone;

    For more details, see also How do I pass more than one array to a subroutine? at Perlfaq Prime.

        --k.


Re: problem : return an array list
by Ryszard (Priest) on Mar 31, 2002 at 05:41 UTC
    This is one of the interesting things in perl. When I 1st started with perl I was weirded out by this quirk. Pretty much you need to pass by reference, then deference it.

    return @ary1, @ary2, @ary3; becomes: return \@ary1, \@ary2, \@ary3.

    Dereference:

    my @aryref = &retary; print ${$aryref[0]}[0]; #print 1st element of 1st array


    As an alternate method to return non order dependent data consider using a hash:
    my %hash; $hash{ary1} = @ary1; $hash{ary2} = @ary2; $hash{ary3} = @ary3;
    I've started return and receiving hashes in my subrou.. ahem, methods, as you now dont have to worry about the order in which you call them..

    As a slight aside Data::Dumper is invaluable when you get the dereferencing blues with complex data structures:

    #!/usr/bin/perl -w use strict; use Data::Dumper; my @aoh; $aoh[0] = { key => 'value'}; print Dumper(@aoh);
    HTH's!
Re: problem : return an array list
by jepri (Parson) on Mar 31, 2002 at 13:59 UTC
    Just to show the upside of this perl feature, you can do:

    foreach ( @array1, @array2, @array3) { ... }

    And perl will step through all the arrays in turn. Also works in function calls and any other place where perl expects an array. Nifty, eh?

    Update: Will work everywhere Perl expects a LIST. Juerd explains below.

    ____________________
    Jeremy
    I didn't believe in evil until I dated it.

      And perl will step through all the arrays in turn. Also works in function calls and any other place where perl expects an array. Nifty, eh?

      Well, nifty - yes. Correct - no.

      With for, perl expects a LIST, not an ARRAY. This is why for (1, 2, 3) also works.

      splice, push, pop, shift and unshift expect an ARRAY as their first argument. The big difference is that where a LIST is expected, an array is flattened as in your example. When an ARRAY is expected, you can only use a single array, not two arrays, not a hash.

      In the case of for (@array1, @array2, @array3), the list passed to for consists of all elements in the three arrays, not the three arrays themselves.

      (Please note that uppercased "LIST" and "ARRAY" are conventions used throughout the Perl documentation)

      U28geW91IGNhbiBhbGwgcm90MTMgY
      W5kIHBhY2soKS4gQnV0IGRvIHlvdS
      ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
      geW91IHNlZSBpdD8gIC0tIEp1ZXJk