[Chicago-talk] Perl 101 question

Andy_Bach at wiwb.uscourts.gov Andy_Bach at wiwb.uscourts.gov
Tue Feb 13 13:44:53 PST 2007


Reviewing a beginner perl course and it says:
You access list values by telling Perl the number of the element you want 
to access ? or its list index. You state the number in square brackets 
after the list.

The list must be enclosed in brackets, and the index in square brackets 
must be enclosed with the list in another level of brackets.

For example, to access the first element of a word list, you use the index 
[0] and enclose both the list and the index in additional brackets.
#!/usr/bin/perl
use warnings;
print (('how', 'price', 'hat') [0]);
print "\n";

So ... I thought that was wrong but:
print ('how', 'price', 'hat') [0];

doesn't work. This does:
my $word =  ('how', 'price', 'hat')[0];
print "$word\n";

as does:
print "", ('how', 'price', 'hat')[0], "\n";

So are the parens just to 'bind' the index to the bare list? 

a

Andy Bach
Systems Mangler
Internet: andy_bach at wiwb.uscourts.gov
VOICE: (608) 261-5738  FAX 264-5932

"Procrastination is like putting lots and lots of commas in the sentence 
of your life."
Ze Frank 
http://lifehacker.com/software/procrastination/ze-frank-on-procrastination-235859.php


More information about the Chicago-talk mailing list