Professional Web Applications Themes

How to pass two arrays as arg in a subroutine? - PERL Beginners

Hi, Whenever you pass two arrays (or any number of arrays for that matter) as arguments to a function, what happens is they get flattened and become a single list and then it is assigned to _. So when U say   array1 has the entire _ (which is now having a single list that was constructed with the 2 arrays passed to the function) For example when you have a piece of code like arr1 = ("Sun", "Mon"); arr2 = ("Tue", "Wed"); arr3 = (arr1, arr2); Now arr3 has a single list with Sun, Mon, Tue and Wed as ...

  1. #1

    Default RE: How to pass two arrays as arg in a subroutine?


    Hi,

    Whenever you pass two arrays (or any number of arrays for that matter)
    as arguments to a function, what happens is they get flattened and
    become a single list and then it is assigned to _.

    So when U say
     

    array1 has the entire _ (which is now having a single list that was
    constructed with the 2 arrays passed to the function)

    For example when you have a piece of code like

    arr1 = ("Sun", "Mon");
    arr2 = ("Tue", "Wed");

    arr3 = (arr1, arr2);

    Now arr3 has a single list with Sun, Mon, Tue and Wed as values. This
    is because of the nature of Perl flattening arrays inside a list.

    A solution for you is to pass the arrays as reference and then
    dereference it inside the function...

    Example:
    ========
    #!/usr/local/bin

    use strict;

    my arr1 = (1, 2, 3, 4);
    my arr2 = ('One', 'Two', 'Three', 'Four');

    &mysub(\arr1, \arr2);

    sub mysub {
    my ($a_1, $a_2) = _;
    for my $var ({$a_1}) {
    print "$var in words is ${$a_2}[$var-1]\n";
    }
    }

    Output:
    =======
    1 in words is One
    2 in words is Two
    3 in words is Three
    4 in words is Four

    Hope this is useful for you.

    For more on references go through "perldoc perlref"

    With Best regards,
    R. Kamal Raj Guptha.


    -----Original Message-----
    From: Edward Wijaya [mailto:a-star.edu.sg]
    Sent: Thursday, August 05, 2004 3:37 PM
    To: org
    Subject: How to pass two arrays as arg in a subroutine?


    Hi,

    I have a subroutine that take 2 arrays as argument.
    I dont' know how to construct it?

    sub mysub{

    my (array1, array2) = _; # is this correct? How do I do it?

    #process array1
    #process array2 etc

    return array3;
    }

    Please advice.

    Thanks so much for your time.

    Regards,
    Edward WIJAYA
    SINGAPORE

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






    Confidentiality Notice

    The information contained in this electronic message and any attachments tothis message are intended
    for the exclusive use of the addressee(s) and may contain confidential orprivileged information. If
    you are not the intended recipient, please notify the sender at Wipro com immediately
    and destroy all copies of this message and any attachments.
    Kamal Guest

  2. #2

    Default Re: How to pass two arrays as arg in a subroutine?

    Have them pass the arrays as a reference. For example:

    array1 = (1, 2, 3, 4, 5);
    array2 = (6, 7, 8, 9, 10);
    mysub(\array1,\array2);

    sub mysub{

    my ($array1, $array2) = _;
    #process {$array1}
    #process {$array2} etc
    return array3;
    }

    Look into perldoc perlref

    Prototyping is another option, not entirely recommended unless it's entirely
    needed. perdoc perlsub for more info.

    --
    -will
    http://www.wgunther.tk
    (the above message is double rot13 encoded for security reasons)

    Most Useful Perl Modules
    -strict
    -warnings
    -Devel::DProf
    -Benchmark
    -B::Dep
    -Data::Dumper
    -Clone
    -Perl::Tidy
    -Beautifier
    -DBD::SQLite
    WilliamGunther@aol.com Guest

  3. #3

    Default Re: How to pass two arrays as arg in a subroutine?

    Thanks so much Kamal,
    your explanation is very clear and complete.
    It works now.
    I learnt a great deal from it.

    Regards
    Edward WIJAYA

     
    Edward Guest

Similar Threads

  1. subroutine definitions
    By Andrew in forum PERL Beginners
    Replies: 25
    Last Post: March 2nd, 10:29 AM
  2. Is there are a way to pass (v)arrays from pl/sql to pro*c?
    By NetComrade in forum Oracle Server
    Replies: 2
    Last Post: November 8th, 07:50 PM
  3. Replies: 0
    Last Post: September 7th, 06:53 PM
  4. How to cate DBI error and pass the control to subroutine?
    By Rob Hanson in forum PERL Beginners
    Replies: 1
    Last Post: August 27th, 07:35 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not 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