Passing several arguments

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Passing several arguments

    Hi all,

    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);
    }

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

    write_index(&read_index);

    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:

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

    sub write_index {
    my ($author_ref, $poem_ref, $alphabet_ref) = shift;
    my @author_index = @$author_ref;
    blablabla
    }

    Any hints appreciated,

    Jan
    --
    These are my principles and if you don't like them... well, I have others. - Groucho Marx
    Jan Eden Guest

  2. Similar Questions and Discussions

    1. #39930 [NEW]: Error passing Word macro arguments
      From: poon dot fung at gmail dot com Operating system: Windows XP PHP version: 5.2.0 PHP Bug Type: COM related Bug...
    2. passing arguments to functions
      Hi all Problem: I want to send 2 arguments to a subroutine in the form of arrays and want to use their result which is also in the form of an...
    3. trouble passing arguments
      I have a RedHat Linux 8.0 system. I have started the httpd (Apache 2.0.40) process and can now fetch pages from the server. I have a test htlm...
    4. Launching shell scripts using PERL and passing arguments
      Hello All, I have a very basic question. I need to launch a Bourne shell script using PERL and automate the responses required by the script. I...
    5. Passing arguments to 'env ruby' on OS X
      When using env on the shebang line, how to I pass arguments to Ruby under OS X? Doing the following gives me an error on OS X, but works on...
  3. #2

    Default Re: Passing several arguments

    Jan Eden wrote:
    > Hi all,
    >
    > 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);
    > }
    >
    > sub write_index {
    > my (%author_indexlines, %poem_lines, @alphabet_index) = shift;
    > blablabla
    > }
    >
    > write_index(&read_index);
    >
    > 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:
    >
    > sub read_index {
    > blablabla
    > return (\%author_indexlines, \%poem_lines, \@alphabet_index);
    > }
    >
    > sub write_index {
    > my ($author_ref, $poem_ref, $alphabet_ref) = shift;
    > my @author_index = @$author_ref;
    > blablabla
    > }
    >
    > Any hints appreciated,
    >
    "Not sure if it might work"... did you try? Trial and error is the best
    way to learn....

    perldoc perlreftut
    perldoc perlref
    perldoc perldsc
    perldoc perllol

    Until you have a strong grasp of references make heavy use of
    Data::Dumper it is your friend and will help you wrangle more complex
    structures.

    May I also suggest to improve your understanding of scoping issues:

    [url]http://perl.plover.com/FAQs/Namespaces.html[/url]

    [url]http://danconia.org[/url]
    Wiggins D'Anconia Guest

  4. #3

    Default Re: Passing several arguments


    On Jan 27, 2004, at 3:26 AM, Jan Eden wrote:
    [..]
    > 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
    [..]

    perldoc -q "How can I pass/return a {Function, FileHandle, Array,"

    remember that your core signature is

    my @return_list = function(@arglist);

    so the only way that you can return more than one
    array or hash will be by passing their refs

    my ($auther, $poem, $index) = function(@arglist);
    ....

    sub function {
    ....
    (\%author_indexlines, \%poem_lines, \@alphabet_index, $etc)
    }

    then you re-ref them as

    while( my ($k,$v) = each %$author )
    {
    ....
    }

    ciao
    drieux

    ---

    Drieux Guest

  5. #4

    Default Re: Passing several arguments

    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, \@alphabet_index);
    > 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]japhy@pobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    <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. ]

    Jeff 'Japhy' Pinyan Guest

  6. #5

    Default Re: Passing several arguments


    Jeff 'japhy' Pinyan wrote:
    >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?
    [snip]
    >>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.
    >
    Ok. I noticed this problem trying to get it to work and did not rethink theway shift works. Thanks!

    - Jan
    --
    There's no place like ~/
    Jan Eden Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139