develooper Front page | perl.beginners | Postings from July 2006

RE: Multidimensional array / Passing an array to a sub

Thread Previous
From:
Charles K. Clarkson
Date:
July 30, 2006 01:01
Subject:
RE: Multidimensional array / Passing an array to a sub
Message ID:
000801c6b3ae$42c95510$0202a8c0@charles
Martin Tournoij wrote:

: I'm currently massivly confused 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.


Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About