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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Intermediate:



Jan 9, 2001, 5:51 AM

Post #1 of 3 (184 views)
reference Can't Post

alright this is a simple question but i just wanna know
if i have an array
@array=(one, two,[three, four])
which ways do i have to 'acces' the item three and could you give me an explanation please
i thought i only could use
but i've also seen
and i know
$$array[2][0] isn't right
but i thought $array[2]->[0] is the same as $$array[2][0]

(i'm sorry for my poor english)


Jan 9, 2001, 3:16 PM

Post #2 of 3 (180 views)
Re: reference [In reply to] Can't Post

Elements in an array can be accessed through numbers, where '0' represents the first element. So to get the third element of an array, you would use $array[2].

#!/usr/bin/perl -w
use strict;
my @numbers = (1..10);
print $numbers[2];

output: 3



Jan 15, 2001, 10:01 AM

Post #3 of 3 (163 views)
Re: reference [In reply to] Can't Post

$array = [one, two,[three, four]];
[] <---- creates a reference to an anonymous array
for reference to an array, a method of dereferencing invloves the use of the -> infix operator. This form of syntactic
sugar that makes it easier to at individual array elements. The type of the derference is determined bu the right operand, that is by what follows directly
after the arrow. If the next thing after the arrow is a bracket, the left operand is treated as a reference to an array, respectively, to be subscripted by an expression on the right.
The arrow is optional between brackets or braces, or between a closing bracket or brace and a parenthesis for an indirect function call.

Each of these next trios is equivalent, corresponding to the three notations we've introduced

$array->[2][0] #three , So $array->[2][0] == $array->[2]->[0]
$$array[2][0] #three , So $$array[2][0] == $$array[2]->[0]
${$array}[2][0] #three , S0 ${$array}[2][0] == ${$array}[2]->[0]

So, @array = ('one', 'two',['three', 'four']);

print $array[2][0] #three , So array[2][0] == array[2]->[0]



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

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