alienhuman has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Help w passing array by ref
by Abigail-II (Bishop) on Oct 10, 2003 at 16:20 UTC | |
|
Re: Help w passing array by ref
by liz (Monsignor) on Oct 10, 2003 at 16:20 UTC | |
|
Re: Help w passing array by ref
by rlb3 (Deacon) on Oct 10, 2003 at 16:14 UTC | |
by kesterkester (Hermit) on Oct 10, 2003 at 16:23 UTC | |
|
Re: Help w passing array by ref
by Not_a_Number (Prior) on Oct 10, 2003 at 17:04 UTC | |
|
Re: Help w passing array by ref
by EvdB (Deacon) on Oct 10, 2003 at 16:22 UTC |