in reply to •Re: subroutines and returning lists...
in thread subroutines and returning lists...

Merlyn & Dmitri: Thank you for your assistance. I had some print statements in the subroutine, so I know that the actual matching of array against hash does work and $ary_element is a defined value before I try to push it. Here is what I'm doing with @_ (this still doesn't work):
sub blah { @array = match_files(\@array, \%hash); print "@array \n"; } sub match_files { @files = (); my($array, $hash) = @_; my(@array) = @$array; my(%hash) = %$hash; foreach $ary_element (@array) { foreach $hash_key( keys ( %hash )) { # do my comparison here } # if i get a match print "$ary_element\n"; # prints defined value push (@files, $ary_element); return @files; } }

Replies are listed 'Best First'.
Re: Re: •Re: subroutines and returning lists...
by The Mad Hatter (Priest) on Apr 03, 2003 at 22:55 UTC
    Your problem is that you try to return multiple times (every loop through the first foreach). This means that you end up returning an array with only one element. Put the return statement just before the end of the sub (after the foreach).

    Added clarification.

      Mad Hatter et al.,

      Thank you very much for your help. Of course it had to be something utterly trivial like having return inside of a loop. doh!

Re: Re: •Re: subroutines and returning lists...
by pg (Canon) on Apr 04, 2003 at 04:37 UTC
    The Mad Hatter has already pointed out the problem. Also it is better to return array ref instead of array. By doing that, you will gain much better performance. If you return array, Perl needs to duplicate the entire array, and that takes time.