Hi Monks
I'm new to references and having a tough time understanding how to use them, even after checking out perlreftut. I'm trying to pass an array to a subroutine makeSql and then iterate over each of the members of the array. For now I'm just printing out the members as a string while I figure out how to get this to work. Later this subroutine will use each of the items in this array as part of a SQL select statement.
#!/usr/bin/perl -w use strict; my @xpid = ("ahuman","alienh","pmonk"); my $sql = makeSql(\@xpid); warn "$sql\n"; sub makeSql { my @xpid = shift; my $return; my $i = 0; foreach my $item (@xpid) { $return .= $xpid[$i]; $i++; } return $return; }
As you probably already know, this warns something like ARRAY(0x8100a38). Can anyone show me how to use references properly to pass an array to a subroutine and iterate through that array? I've figured out that w my current code I can delete the for loop, assign the global \@xpid to a scalar and do something like $return = $xpid[0] and get a value... but how do I work w/ an array w/out using the global?
AH
Using perl 5.6.1 unless otherwise noted. Apache 1.3.27 unless otherwise noted. Redhat 7.1 unless otherwise noted.
In reply to Help w passing array by ref by alienhuman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |