LMPX.COM |
Home | Linux | Mysql | PHP | XML | ||
|
|
|||
From: Gunnar Hjalmarsson Date: Wed May 14 16:57:33 2008 Subject: Re: items in array
[ Please do not top-post! ] Tech list wrote: > Dr.Ruud wrote: >> "Li, Jialin" schreef: >>> On Wed, May 14, 2008 at 4:01 PM, Tech list >>> <tech_list@womenshealth.com> wrote: >>>> what is the correct way to get the number of items in an array? >>>> >>>> I used to use $#array >>> >>> $#array is the index of the last item, so the number should be >>> $#array + 1, or >>> scalar @array >> >> Please read perlvar about $[. Normally it is 0, but you can set it to a >> different value, like -3 or 27. >> >> The $#array is the last index of @array, so $array[$#array] is >> equivalent to $array[-1]. >> >> The number of items in an array is returned by @array in scalar context, >> examples: >> >> scalar(@array) >> >> (0+ @array) >> >> if (0 < @array) { ... } >> >> if (@array) { ... } > > Is $#array deprecated or not? It's not deprecated. But, as others already have explained, it does not represent the number of items in an array, which is what you asked about, after all. -- Gunnar Hjalmarsson Email: http://www.gunnar.cc/cgi-bin/contact.pl
| Navigate in group perl.beginners at sever nntp.perl.org | |
| Previous | Next |
| © No Copyright You are free to use Anything |
Site Maintained by PHP Developer
Powered By PHP Consultants |