Home > Archive > PERL Beginners > July 2006 > Multidimensional array / Passing an array to a sub
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Multidimensional array / Passing an array to a sub
|
|
| Martin Tournoij 2006-07-30, 3:57 am |
| I'm currently massivly by multidimensional arrays and passing =
arrays to subs.
Why is it that @_[1] isn't properly copied to @a?
I'm not sure what exactly I'm doing wrong here, probably something simpl=
e, =
but I can't find anything that works in the manpages or on the site...
Music($var1, \@array);
sub Music
{
print $_[1][1]; # works
my @a =3D @_[1] # Gives warning, should be written as $_[1]
print $a[1]; # Outputs ARRAY(#memaddr)
print $a[1][1]; # Works
for (@a)
{
print; # Outputs ARRAY(#memaddr)
}
for (@_[1])
{
print; # Also outputs ARRAY(#memaddr)
}
}
| |
| Charles K. Clarkson 2006-07-30, 3:57 am |
| Martin Tournoij wrote:
: I'm currently massivly by multidimensional arrays and
: passing arrays to subs.
:
: Why is it that @_[1] isn't properly copied to @a?
It is properly copied. Because @_[1] one is a scalar and
@a is an array the reference to the passed array in stored in
$a[0].
: Music($var1, \@array);
:
: sub Music
: {
: print $_[1][1]; # works
: my @a = @_[1] # Gives warning, should be written as $_[1]
So stop writing it as @_[1], silly. :)
my $var = shift;
my $array_ref = shift;
: print $a[1]; # Outputs ARRAY(#memaddr)
print @{ $array_ref };
: print $a[1][1]; # Works
:
: for (@a)
for ( @{ $array_ref } ) {
HTH,
Charles K. Clarkson
--
Mobile Homes Specialist
Free Market Advocate
Web Programmer
254 968-8328
Don't tread on my bandwidth. Trim your posts.
| |
| Thomas J. 2006-07-30, 3:57 am |
| Martin Tournoij schrieb:
>
> Music($var1, \@array);
>
> sub Music
> {
> print $_[1][1]; # works
> my @a = @_[1] # Gives warning, should be written as $_[1]
yes, it should be written as $_[1] , if you want the ref of your
@array.
perl doesnt know what you want here, but i think you want:
my @a = @{ $_[1] };
or something similar.
all of your errors will gone after that.
>
> print $a[1]; # Outputs ARRAY(#memaddr)
with my suggestion the line one down wont work anymore
the line above will probably do what you want.
> print $a[1][1]; # Works
>
> for (@a)
> {
> print; # Outputs ARRAY(#memaddr)
> }
>
with my suggestion the line one down has also be changed to:
for ( @{ $_[1] } )
> for (@_[1])
> {
> print; # Also outputs ARRAY(#memaddr)
> }
> }
if you want the structure of a reference you have to dereference it.
see: perldoc perlref
hope this helps
Thomas
| |
| John W. Krahn 2006-07-30, 7:56 am |
| Martin Tournoij wrote:
> I'm currently massivly by multidimensional arrays and passing
> arrays to subs.
>
> Why is it that @_[1] isn't properly copied to @a?
>
> I'm not sure what exactly I'm doing wrong here, probably something
> simple, but I can't find anything that works in the manpages or on the
> site...
>
> Music($var1, \@array);
>
> sub Music
> {
> print $_[1][1]; # works
$_[1] contains a reference to @array so $_[1][1] is the same as $array[1] (or
the second element of @array.)
> my @a = @_[1] # Gives warning, should be written as $_[1]
@_[1] is an array slice, the warning is because the list [1] has only one
element, it's the same as saying "$a[0] = @_[1]". If you want to copy @array
to @a then you have to dereference it properly:
my @a = @{ $_[1] }
But if you are going to copy the array anyway you could just do it like this:
Music($var1, @array);
sub Music
{
my ( $var, @a ) = @_;
John
--
use Perl;
program
fulfillment
| |
| Paul Lalli 2006-07-30, 6:57 pm |
| Charles K. Clarkson wrote:
> Martin Tournoij wrote:
>
> : I'm currently massivly by multidimensional arrays and
> : passing arrays to subs.
> :
> : Why is it that @_[1] isn't properly copied to @a?
>
> It is properly copied. Because @_[1] one is a scalar
No. It's not. And that's why you get the warning.
@_ is an array.
$_[1] is a scalar - the second element of the array @_
@_[1] is an array slice - the list containing the second element of the
array @_
Paul Lalli
|
|
|
|
|