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

  1. #1

    Default passing an array

    Hello,

    what is the best way to pass an array to a sub routine,
    IE.

    my @fields = qw(one two three);

    send_array(@fields);


    sub send_array {
    my @ary = @_;
    # do stuff here....
    }

    is this the most effective way to pass an array to sub
    routine or is there a better way to do this.

    thx's

    --
    Mike<mickalo>Blezien
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    Thunder Rain Internet Publishing
    Providing Internet Solutions that work!
    [url]http://www.thunder-rain.com[/url]
    Quality Web Hosting
    [url]http://www.justlightening.net[/url]
    MSN: [email]mickalo@thunder-rain.com[/email]
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

    Mike Blezien Guest

  2. Similar Questions and Discussions

    1. Passing value to Array
      If I've got x = "test5" How can I put the value of x to Array list=; I'd try to use ---> list= but is not work.
    2. passing array in the url
      Hello there, I am unable to pass an array thought he url. Even the most simple array as in the code: $test = array(1,2,3,4); echo "<a...
    3. Array passing problem...
      Hoping someone can point out my more than likely simple mistake here... function getBranchArray($desig,$branchArray){ $arrayResult =...
    4. Passing Array Via CGI.pm
      Hello all, I'm a Perl newbie here and have a quick question regarding CGI.pm. I have a CGI script that passes an array to another CGI script as...
    5. Passing a keyed Array via php.
      Hi ACteon! On 18 Aug 2003 19:17:23 -0700, acteon@yahoo.com (Acteon) wrote: Use - form method="post" - $_POST etc. - no books as they are...
  3. #2

    Default Re: passing an array

    On Dec 9, 2003, at 8:49 PM, Mike Blezien wrote:
    > Hello,
    >
    > what is the best way to pass an array to a sub routine,
    > IE.
    >
    > my @fields = qw(one two three);
    >
    > send_array(@fields);
    >
    >
    > sub send_array {
    > my @ary = @_;
    > # do stuff here....
    > }
    >
    > is this the most effective way to pass an array to sub routine or is
    > there a better way to do this.
    That depends on how you define "effective". The way you showed is the
    easiest. References are probably better in some respects though, if
    you don't mind the complications:

    my @array = qw(1 2 3);
    my @other_array = qw(dog cat pig);

    send_array(\@array, \@other_array);

    sub send_array {
    my($nums, $anims) = @_;
    # do stuff with references here, for example

    print $nums->[1], ' ', $anims->[2], "\n"; # prints "2 pig"
    }

    Hope that helps.

    James

    James Edward Gray II Guest

  4. #3

    Default Re: passing an array

    Thanks James,

    passing as a reference was basically what I was trying to
    recall,.... your example made that clear :)



    --
    Mike<mickalo>Blezien
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
    Thunder Rain Internet Publishing
    Providing Internet Solutions that work!
    =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

    James Edward Gray II wrote:
    > On Dec 9, 2003, at 8:49 PM, Mike Blezien wrote:
    >
    >> Hello,
    >>
    >> what is the best way to pass an array to a sub routine,
    >> IE.
    >>
    >> my @fields = qw(one two three);
    >>
    >> send_array(@fields);
    >>
    >>
    >> sub send_array {
    >> my @ary = @_;
    >> # do stuff here....
    >> }
    >>
    >> is this the most effective way to pass an array to sub routine or is
    >> there a better way to do this.
    >
    >
    > That depends on how you define "effective". The way you showed is the
    > easiest. References are probably better in some respects though, if you
    > don't mind the complications:
    >
    > my @array = qw(1 2 3);
    > my @other_array = qw(dog cat pig);
    >
    > send_array(\@array, \@other_array);
    >
    > sub send_array {
    > my($nums, $anims) = @_;
    > # do stuff with references here, for example
    >
    > print $nums->[1], ' ', $anims->[2], "\n"; # prints "2 pig"
    > }
    >
    > Hope that helps.
    >
    > James
    >
    >
    >
    Mike Blezien Guest

  5. #4

    Default RE: passing an array

    Mike Blezien wrote:
    > Hello,
    >
    > what is the best way to pass an array to a sub routine, IE.
    You can't pass an array to a sub. You can only pass a list of scalars.
    >
    > my @fields = qw(one two three);
    >
    > send_array(@fields);
    >
    >
    > sub send_array {
    > my @ary = @_;
    Whatever you passed ends up in @_. There's no way for the sub to know how
    this data was passed, if it matters.
    > # do stuff here....
    > }
    >
    > is this the most effective way to pass an array to sub
    > routine or is there a better way to do this.
    If your sub just needs a list of values, what you've done is fine. If your
    sub somehow needs to know about the @fields array, perhaps to modify it or
    add or remove elements, you have to pass an array reference. Then you
    manipulate the array via the reference inside the sub.
    Bob Showalter 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