Array Size
Author Message
Array Size

I am no sure if this is asked before but after going thru the whole

Q : How do i determine the size of an array as in how many elements

Azman

Fri, 22 Dec 2000 03:00:00 GMT
Array Size

Quote:

> \$#array will give you the last element.

The subscript of the last element.

\$array[\$#array];

will give you the last element.

Jeremy
--
Jeremy D. Zawodny                   Web Geek, Perl Hacker, etc.

Fri, 22 Dec 2000 03:00:00 GMT
Array Size

Quote:

> I am no sure if this is asked before but after going thru the whole

> Q : How do i determine the size of an array as in how many elements

\$arr_size = \$#array + 1;

Of course, there's more than one way to do it.

Jeremy
--
Jeremy D. Zawodny                   Web Geek, Perl Hacker, etc.

Fri, 22 Dec 2000 03:00:00 GMT
Array Size

Quote:

> I am no sure if this is asked before but after going thru the whole

> Q : How do i determine the size of an array as in how many elements

Evaluating a list or an array in a scalar context will return the
number of elements.  For example:

After this statement, `\$a' will contain an integer representing the

There are times in Perl where something might mistakenly be thought to
be evaluated in a scalar context, when in actuality, it isn't.  To
ensure that you get a scalar context when you really want one, you can
do this:

Again, `\$a' will contain an integer representing the number of

In my Perl programs, I tend to use the `scalar' operator whenever I
want to get the number of elements in a list or an array.  This is
often redundant, but it makes it clear to those people in the future
who might be trying to support my code that I'm intentionally wanting
a scalar context.  Therefore, I recommend the use of `scalar' in cases
such as this.

--

perl -e '\$n=170;for(\$d=2;(\$d*\$d)<=\$n;\$d+=(1+(\$d%2))){for(\$t=0;(\$n%\$d)==0;

Fri, 22 Dec 2000 03:00:00 GMT
Array Size

Quote:

> > I am no sure if this is asked before but after going thru the whole
> > newsgroup, I have not found an question based on this.

> > Q : How do i determine the size of an array as in how many elements

> \$arr_size = \$#array + 1;

> Of course, there's more than one way to do it.

whenever i see this i think it warrants an explanation that obtaining
the size in this manner, though typically safe, is coincidental. i
know it's rare to see code that actually modifies \$[, but i think it's
important to note that a more consistent method would be:

\$arr_size = \$#array + 1 - \$[;

which of course is uglier and more cumbersome than the very clean:

cheers,

--

"THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER."
- Cosmic AC

Fri, 22 Dec 2000 03:00:00 GMT
Array Size

[ posted and mailed ]
-> I am no sure if this is asked before but after going thru the whole
->
-> Q : How do i determine the size of an array as in how many elements

Please pick up a copy of `Learning Perl' or `Programming Perl'. All of the
questions you have asked in the last day or so are answered in both. If you
are strapped for cash, the answers can also be found in the FREE documentation
and FAQs that came with your perl distribution.

\$#array will give you the last element.

HTH

Bob Trieger

" Cost a spammer some cash: Call 1-800-286-0591
and let the jerk that answers know that his
toll free number was sent as spam. "

Sat, 23 Dec 2000 03:00:00 GMT
Array Size

[courtesy cc of this posting sent to cited author via email]

In comp.lang.perl.misc,

:Evaluating a list or an array in a scalar context will return the
:number of elements.

No, sir.  Not for the "list" notion.  There *are* no lists
in scalar context.

--tom
--
You are the sponsor for the account andrew (Andrew Hume).

Sat, 23 Dec 2000 03:00:00 GMT
Array Size

Quote:
> I am no sure if this is asked before but after going thru the whole

So instead of doing some work yourself, you decided that you would let
us do the work? If you don't want to read all messages, just refer to
the perl faq and the perl documentation. If you then can't find it,
use a news archive line dejanews to find out if it has been asked
before. If you still can't find it, ask here. What makes you think we
don't mind doing your work for you?

Quote:
> Q : How do i determine the size of an array as in how many elements

This is in the perl documentation, in one of the documents that you
really _should_ read before even attempting programming in perl.

# perldoc perldata
[snip]

[snip]
If you evaluate a named array in a scalar context, it
returns the length of the array.  (Note that this is not
true of lists, which return the last value, like the C comma
operator.)  The following is always true:
[snip]

You see, if you had just read a few documentation pages, you would
have known.

Basic set of pages to read:

# perldoc perl

This will list all documentation, in an order which is roughly the
same as my list:

# perldoc perlsyn
# perldoc perldata
# perldoc perlop
# perldoc perlfunc

And of course:

# perldoc perlfaq

and related pages.

Slightly more advanced, but still basic knowledge:

# perldoc perlsub
# perldoc perlvar
# perldoc perlsub
# perldoc perlre
# perldoc perlmodlib

You can also buy a good book. http://www.perl.com has a good list

Martien
--
Martien Verbruggen                  |
Commercial Dynamics Pty. Ltd.       | accept as reality - Calvin
NSW, Australia                      |

Sat, 23 Dec 2000 03:00:00 GMT
Array Size

Quote:
> Evaluating a list or an array in a scalar context will return the
> number of elements.  For example:

# perldoc perldata
[snip]
If you evaluate a named array in a scalar context, it
returns the length of the array.  (Note that this is not
true of lists, which return the last value, like the C comma
operator.)
[snip]

I recall someone here saying (you can guess who): 'There is no such
thing as a list in a scalar context'.

Martien
--
Martien Verbruggen                  |
Webmaster www.tradingpost.com.au    | That's funny, that plane's dustin'
Commercial Dynamics Pty. Ltd.       | crops where there ain't no crops.
NSW, Australia                      |

Sat, 23 Dec 2000 03:00:00 GMT
Array Size

:
: > \$#array will give you the last element.
:
: The subscript of the last element.
:
: \$array[\$#array];
:
: will give you the last element.

Or, using a notation I find more pleasing, \$array[-1] does the same thing.
One less copy of the array name to mistype. :)

---------------------------------------------------------------------

|      Member of The HTML Writers Guild: http://www.hwg.org/
"Every man and every woman is a star."

Sat, 23 Dec 2000 03:00:00 GMT
Array Size
: Or, using a notation I find more pleasing, \$array[-1] does the same thing.
: One less copy of the array name to mistype. :)

same way. :-)

--
-Zenin

Sun, 24 Dec 2000 03:00:00 GMT

 Page 1 of 1 [ 11 post ]

Relevant Pages