develooper Front page | perl.beginners | Postings from February 2002

Re: Arrays 1x3 or 3x1 - The real questions

Thread Previous | Thread Next
Brett W. McCoy
February 12, 2002 08:58
Re: Arrays 1x3 or 3x1 - The real questions
Message ID:
On Tue, 12 Feb 2002, Steven M. Klass wrote:

> Let's start off with some simple code..
> my $arg = &SomeFunction ( my @arry = qw/one two three/)
> sub SomeFunction {
> 	my @array = @_[0];

No, you are only grabbing the first element of @_.  You should either pass
the array as a reference (best way), or just grab up the entire @_.
Keep in mind that if you pass an array and any scalars as arguments, they
will all be flattened out into @_, as a single list.  This is why passing
a reference is better, to differentiate lists and scalars.

SomeFunction([qw(one two three)]);

sub SomeFunction {
	my $array = shift;
	foreach(@{$array}) {
	  print "$_\n";

-- Brett
Removing the straw that broke the camel's back does not necessarily
allow the camel to walk again.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About