passing arg to sub problem

S

slash

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
 
K

Kerry Colligan

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

or

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

nobull

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.

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.
 
A

Aditya

myFunc(\@arrayOne, \@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(\@arrayOne, \@arrayTwo);
my @array1 = @$str1;
my @array2 = @$str2;
......
(Your myFunc wud still remain the same)


This should work..it worked for me..

Aditya...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top