passing arg to sub problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • slash

    passing arg to sub problem

    hi,
    i am new to perl and am trying to convert the code below into a
    subroutine but have a problem concering the reading of the input.
    i am trying to pass an array as an argument to the subroutine. the
    array is produced earlier in the program and needs to be passed to the
    subroutine. @array holds LINES of words separated by whitespace. so,
    $array[0] will be one line from the DATA below. the code below runs
    fine as a separate program but I don't know how to pass @array as an
    argument to the function. could someone help me with this simple
    problem? i hope i worded it clearly.

    thanks,
    slash

    ----CODE-----------
    while (<>) {
    @tmp=split;
    push @AoA, [@tmp];
    }
    for $row(@AoA) {
    push @$row, $ARGV;
    print "@$row\n";
    }


    -----DATA------

    regular expressions are used by
    many programs such as the
    UNIX commands grep sed awk

    ------

    thanks,
    slash
  • Kerry Colligan

    #2
    Re: passing arg to sub problem

    Something like:
    showMe( $arr );
    sub showMe{
    my $ln = shift;
    ...
    }

    or

    showMe( @arr );
    sub showMe {
    my($foo, $bar) = @_;
    ...
    }


    "slash" <satishi@gwu.ed u> wrote in message
    news:30fe9f1e.0 308111209.55e66 78b@posting.goo gle.com...[color=blue]
    > hi,
    > i am new to perl and am trying to convert the code below into a
    > subroutine but have a problem concering the reading of the input.
    > i am trying to pass an array as an argument to the subroutine. the
    > array is produced earlier in the program and needs to be passed to the
    > subroutine. @array holds LINES of words separated by whitespace. so,
    > $array[0] will be one line from the DATA below. the code below runs
    > fine as a separate program but I don't know how to pass @array as an
    > argument to the function. could someone help me with this simple
    > problem? i hope i worded it clearly.
    >
    > thanks,
    > slash
    >
    > ----CODE-----------
    > while (<>) {
    > @tmp=split;
    > push @AoA, [@tmp];
    > }
    > for $row(@AoA) {
    > push @$row, $ARGV;
    > print "@$row\n";
    > }
    >
    >
    > -----DATA------
    >
    > regular expressions are used by
    > many programs such as the
    > UNIX commands grep sed awk
    >
    > ------
    >
    > thanks,
    > slash[/color]



    Comment

    • nobull@mail.com

      #3
      Re: passing arg to sub problem

      satishi@gwu.edu (slash) wrote in message news:<30fe9f1e. 0308111209.55e6 678b@posting.go ogle.com>...
      [color=blue]
      > I don't know how to pass @array as an
      > argument to the function. could someone help me with this simple
      > problem? i hope i worded it clearly.[/color]

      This is FAQ: "How can I pass/return a {Function, FileHandle, Array,
      Hash, Method, Regex}?"

      This newsgroup does not exist (see FAQ). Please do not start threads
      here.

      Comment

      • Aditya

        #4
        Re: passing arg to sub problem

        myFunc(\@arrayO ne, \@arrayTwo); # here you are passing the array as reference
        sub myFunc{
        my ($arrayOneRef, $arrayTwoRef) = @_; # accepting the array as a string
        my @array1 = @$arrayOneRef; # derefrencing it back into an array
        my @array2 = @$arrayTwoRef;
        ....
        }

        OR

        In this case you are storing the values back into an array..
        my ($str1, $str2) = myFunc(\@arrayO ne, \@arrayTwo);
        my @array1 = @$str1;
        my @array2 = @$str2;
        ......
        (Your myFunc wud still remain the same)


        This should work..it worked for me..

        Aditya...

        Comment

        Working...