[oak perl] newbie array question

Jon Seidel, CMC jseidel at edpci.com
Wed Jan 5 12:25:00 CST 2005


Sandy...

David Fetter gave you a good answer, which I can add to.

First, the "print @arrayname;" stuff didn't work because print expects
list context, not scalar. So it prints out the contents of the array all
squished together. That's why David said "print scalar(@arrayname);"

Second, I do use the $#array+1 construct just to make sure (for myself)
that I've got it right... I still have some confusion over the context
situations. So I would say go ahead and use this explicit expression if it
makes you comfortable. TMTOWTDI.

..jon

> Hi, I'm a very new newbie learning arrays.  Apologies for
> 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
>
> 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?
>
> Is this the only way to do it...
>
> print scalar @arrayname;
>
> ???
>
> Seems longer than
>
> print $#array + 1;
>
> (if that even works)
>
> So I'm a little confused, partly because I'm reading a chapter
> on arrays and the print command is used in almost none of
> the coding examples.
>
> Any help appreciated.  Thanks.
>
> --Sandy Santra
>
>
> _______________________________________________
> Oakland mailing list
> Oakland at mail.pm.org
> http://mail.pm.org/mailman/listinfo/oakland
>



-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

      Connecting Business and Technology

Jon Seidel, CMC                   +1-510-530-6314
EDP Consulting, Inc.                www.edpci.com

CMC (Certified Management Consultant) is a certification mark awarded by
the Institute of Management Consultants USA and represents evidence of the
highest standards of consulting and adherence to the ethical canons of the
profession. Less than 1% of all consultants have achieved this level of
performance. See www.imcusa.org/hireacmc.acgi.



More information about the Oakland mailing list