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

I have this call:

@users = @{GetUsers()}; map { $tk{user_list}->insert('end', -itemtype=>'imagetext', -text=>"$_", -image=>$im{ONE}) } @users;


and the GetUsers subroutine is:

sub GetUsers { my $data; my $field; my @found; foreach my $item (@list) { while (my ($field, $data) = each %{$item}) { next if $field !~ /account/i; if (Win32::NetAdmin::UsersExist('',$data)) { print "This '$data' is a user account\n"; push (@found, $data); } } } #return @found; # this does not return correctly. #return [qw/One Two Three/]; #this will return correctly. }
Obviously I have a problem with returning an array, I need to return the @found array. I am not sure why the return qw/One Two Three/ works and the @found doesn’t.

Replies are listed 'Best First'.
Re: Returning array values
by DamnDirtyApe (Curate) on Jul 29, 2002 at 08:37 UTC

    This

    #return @found; # this does not return correctly.

    returns an array a list. This

    #return [qw/One Two Three/]; #this will return correctly.

    returns an array reference. I recommend you go with

    return \@found; # this ought to work.

    Update: corrected first return type. Thanks podmaster.


    _______________
    D a m n D i r t y A p e
    Home Node | Email
Re: Returning array values
by PodMaster (Abbot) on Jul 29, 2002 at 08:41 UTC
    You do not have a problem returning an array. You can only return a list, or a reference to an array, commonly referred to as an arrayref.
    my @Array = ( 1, 2, 3 ); my $ArrayRef = [ 1, 2, 3 ]; $ArrayRef = \@Array; ## How do you "de-reference" an array reference? @Array = @$ArrayRef; @Array = @{$ArrayRef};
    Now I suggest you go and read perldata and perlref, until it all makes sense to you. These are perl basics, and you need to know them by heart.

    update:

    return @array; # DOES NOT RETURN AN ARRAY, IT RETURNS A LIST
    Besides knowing the basics perl data structures and references (perl basics), the other important thing to know about is context. japhy wrote an excellent article on that: "List" is a Four-Letter Word.

    ____________________________________________________
    ** The Third rule of perl club is a statement of fact: pod is sexy.

      I believe that if you remove the second @ and the ()
      from around the call to GetUsers():
       @users = @{GetUsers()};
      leaving you with  @users = GetUsers();
      your code should work the way you expected in the first place.
Re: Returning array values
by valdez (Monsignor) on Jul 29, 2002 at 08:40 UTC

    This one returns an array

    #return @found; # this does not return correctly.

    This returns a reference to an array

    #return [qw/One Two Three/]; #this will return correctly.

    What you need is

    return \@found;

    that transforms your array into a reference, as needed by the code that calls GetUsers

    Ciao, Valerio

      Thanks Guys indeed, the \ @found has worked.

      I also have tried getting rid of the @{} so the returned value is a single array which also worked, however I will be needing a "list of lists" later on in my script, so I need to use the \ reference.

      PS: I have read the ‘perldsc’ a number of times but I guess that I am a bit of a slow ‘Scribe’ and there were no examples on referencing on this tutorial. However, amongst your replies, I have enough articles to go on and improve my Perl techniques.

      Cheers.

        You are welcome :-)

        My way to understand Perl dsc was to Data::Dumper my structures, it is simpler if you visualize them!

        Happy coding, Valerio