CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
about array in array

 



makals
Novice

Jan 7, 2011, 9:13 PM

Post #1 of 3 (839 views)
about array in array Can't Post

hello

see this:

$ref = [ 1, 2, [ 10, 20 ] ];
$element = {$ref->[2]}->[1];


what is it mean the [2] and [1] ?




(This post was edited by makals on Jan 7, 2011, 9:19 PM)


BillKSmith
Veteran

Jan 8, 2011, 7:50 AM

Post #2 of 3 (821 views)
Re: [makals] about array in array [In reply to] Can't Post

Your first statement creates an anonymous array consisting of three elements ( '1', '2', and a reference to another array which contains two elements '10' and '20'). A reference to this array is assigned to the scalar $ref.

Your second statement accesses the '20':

$ref->[2] accesses the third element of the array referenced by $ref. This element is a reference.

{...}->[1] accesses the second element of the array referenced by that reference.

Note: the 1 and 2 are array indexes (which start at zero).



Refer: perldoc perlref
Good Luck,
Bill


shawnhcorey
Enthusiast


Jan 8, 2011, 8:40 AM

Post #3 of 3 (818 views)
Re: [makals] about array in array [In reply to] Can't Post


In Reply To
hello

see this:

$ref = [ 1, 2, [ 10, 20 ] ];
$element = {$ref->[2]}->[1];


what is it mean the [2] and [1] ?



It means nothing.

Code
perl -e'$ref = [ 1, 2, [ 10, 20 ] ]; 
> $element = {$ref->[2]}->[1]; '
Not an ARRAY reference at -e line 2.


Perhaps you meant

Code
perl -e'$ref = [ 1, 2, [ 10, 20 ] ]; 
> $element = $ref->[2]->[1]; '


Though it is commonly written as

Code
perl -e'$ref = [ 1, 2, [ 10, 20 ] ]; 
> $element = $ref->[2][1]; '


__END__

I love Perl; it's the only language where you can bless your thingy.

Perl documentation is available at perldoc.perl.org. The list of standard modules and pragmatics is available in perlmodlib.

Get Markup Help. Please note the markup tag of "code".

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives