[oak perl] newbie array question

David Fetter david at fetter.org
Wed Jan 5 00:07:07 CST 2005


On Wed, Jan 05, 2005 at 06:00:00AM +0000, Sandy Santra wrote:
> Hi, I'm a very new newbie learning arrays.  Apologies for

Welcome!

> the length of this--it's all just a little hard to grasp at this
> point. 
> 
> I have a perlintro document that says don't bother using
> 
> $#array + 1

You could use 

my $count = scalar(@array);

if you need it explicitly.  However, you seldom will.  To iterate over
an array, you can just do

foreach my $element (@array) {
    # your fun stuff here.
}

> for determining the number of items in an array, implying
> there's a shortcut.  But it doesn't really give me a simple(r)
> way of doing it. 
> 
> I just want the print command to produce the number of items,
> but none of the following work: 
> 
> print @arrayname;
> print '@arrayname';
> print "@arrayname";
> 
> I was hoping at least the first of those would, since I
> thought something without quotes usually implied same was
> a number, and Perl is supposed to give me a number when
> it sees @arrayname in a scalar context, right? 

Um, don't worry about that just this minute.  See above :)

Cheers,
D
-- 
David Fetter david at fetter.org http://fetter.org/
phone: +1 510 893 6100   mobile: +1 415 235 3778

Remember to vote!


More information about the Oakland mailing list