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

thanks a lot guys... i was not aware that the last element of an array would consume the hash reference. (thanks linuxer) i have swaped the location of the array with that of the hash for now, but the suggestions from bichonfrise and johngg is cleaner and does not interfere with what i am trying to do... thanks again.
  • Comment on Passing array and hash together : Solved

Replies are listed 'Best First'.
Re: Passing array and hash together
by johngg (Canon) on Apr 20, 2009 at 21:05 UTC

    In your routine_2 the @array consumes all of the remaining elements of @_ because the routine has no idea of where the array ends and the hash reference starts. You could either change the order so the the hash reference comes before the array or, probably better, change the array to an array reference.

    ... routine_2( $num, \ @array, \ %fruit ); ... sub routine_2 { my( $num, $aref, $href ) = @_; ...

    I hope this is helpful.

    Cheers,

    JohnGG

Re: Passing array and hash together
by linuxer (Curate) on Apr 20, 2009 at 20:56 UTC

    Please use <code></code> tags to format your code.

    In routine_2 your hash reference is stored as the last element in @array.

    quoted from perldata

    The final element of a list assignment may be an array or a hash: ($a, $b, @rest) = split; my($a, $b, %rest) = @_; You can actually put an array or hash anywhere in the list, but the fi +rst one in the list will soak up all the values, and anything after it will become undefin +ed. This may be useful in a my() or local().

    You must decide now, whether to pass the array as last argument to routine_2 or to pass a reference to that array and then use that reference in your subroutine. I'd use the second option. ;o)

Re: Passing array and hash together
by bichonfrise74 (Vicar) on Apr 20, 2009 at 20:58 UTC
    Is this what you want?
    &routine_2( $num, \@array, \%fruit); ... sub routine_2 { my($num, $array, $ref) = @_; print ("debug: routine_2: array = @$array \n"); ... }