On Jan 27, Jan Eden said:

>I have a subroutine which fills into several hashes and arrays
>(%author_indexlines, %poem_lines, @alphabet_index etc). How can I return
>those variables and pass them to another subroutine? This does not work,
>of course
>
>sub read_index {
>    blablabla
>    return (%author_indexlines, %poem_lines, @alphabet_index);
> }

You most CERTAINLY want to return references to those.

  sub read_index {
    ...
    return \(%author_indexlines, %poem_lines, @alphabet_index);
  }

That last line is equivalent to

    return (\%author_indexlines, \%poem_lines, [EMAIL PROTECTED]);

> sub write_index {
>    my (%author_indexlines, %poem_lines, @alphabet_index) = shift;
>}

That is just pure nonsense.  First of all, shift() only returns ONE
element from an array.  Second, you can't assign to multiple aggregates at
once (aggregates meaning arrays and hashes).  You'll need to use
references here too:

  sub write_index {
    my ($authref, $poemref, $alpharef) = @_;
    my %author_indexlines = %$authref;
    my %poem_lines = %$poemref;
    my @alphabet_index = @$alpharef;
    ...
  }

>I thought that this might be achieved returning references to the
>respective arrays and hashes and dereference them after passing, but
>since the elements have local scope in the read_index sub, I am not sure
>if this might work:

You guessed almost correctly.  You still have 'shift', though.  shift()
will only return ONE element.  Getting all of them from @_ is the way to
do it.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to