For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > October 2006 > Non-deprecated way to capture array length









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Non-deprecated way to capture array length
Marc Sacks

2006-10-12, 6:59 pm

I needed to find out the length of an array and did it by referencing
the array in scalar context. However, "use warnings" indicated that this
is a deprecated feature. Is there a non-deprecated way to do this
(other than looping through the whole array until I run out of
elements)? Thanks.

--
Marc Sacks, Ed.D. | Quality Assurance Manager
Lextranet | 107 Union Wharf | Boston, MA 02109
http://www.lextranet.com
(617) 227-4469 Extension 228
msacks@lextranet.com

THE INFORMATION IN THIS MESSAGE IS INTENDED ONLY FOR THE PERSONAL AND CONFIDENTIAL USE OF THE DESIGNATED RECIPIENTS NAMED ABOVE AND MAY CONTAIN LEGALLY PRIVILEGED INFORMATION. If the reader of this message is not the intended recipient or an agent respons
ible for delivering it to the intended recipient, you are hereby notified that you have received this document in error, and that any review, dissemination, distribution or copying of this message is strictly prohibited. If you have received this communic
ation in error, please notify us immediately by telephone at 617-227-4469 Ext. 228. Thank you.


Jeff Pang

2006-10-12, 6:59 pm


>I needed to find out the length of an array and did it by referencing
>the array in scalar context. However, "use warnings" indicated that this
>is a deprecated feature. Is there a non-deprecated way to do this
>(other than looping through the whole array until I run out of
>elements)? Thanks.
>


How do you write it? I can get it like:

$ perl -Mstrict -Mwarnings -le 'my @arr=qw/aa bb cc/;print length(join "",@arr)'
6

--
Jeff Pang
NetEase AntiSpam Team
http://corp.netease.com
Derek B. Smith

2006-10-12, 6:59 pm

--- Marc Sacks <msacks@lextranet.com> wrote:

> I needed to find out the length of an array and did
> it by referencing
> the array in scalar context. However, "use warnings"
> indicated that this
> is a deprecated feature. Is there a non-deprecated
> way to do this
> (other than looping through the whole array until I
> run out of
> elements)? Thanks.
>


Hi Mark,

the best practice/standard way to get an array length
is:

print scalar @array_name,"\n";
or
my $array_lnght = scalar @array_name;

________________________________________
__________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
John W. Krahn

2006-10-12, 6:59 pm

Marc Sacks wrote:
> I needed to find out the length of an array and did it by referencing
> the array in scalar context. However, "use warnings" indicated that this
> is a deprecated feature. Is there a non-deprecated way to do this
> (other than looping through the whole array until I run out of
> elements)? Thanks.


Using an array in scalar context is *not* deprecated.

Since you haven't shown any code I would have to guess that you are using
defined() on an array which *is* deprecated.



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
Jeff Pang

2006-10-12, 6:59 pm


>
>
>How do you write it? I can get it like:
>
>$ perl -Mstrict -Mwarnings -le 'my @arr=qw/aa bb cc/;print length(join "",@arr)'
>6
>


Sorry, if you mean the number of elements in an array,then it should be:
my $num = scalar @array;

--
Jeff Pang
NetEase AntiSpam Team
http://corp.netease.com
Kim Helliwell

2006-10-12, 6:59 pm


-----Original Message-----
From: Jeff Pang [mailto:pangj@earthlink.net]=20
Sent: Monday, October 09, 2006 7:48 AM
To: beginners
Subject: Re: Non-deprecated way to capture array length


>
this=20[color=darkred]
>
>How do you write it? I can get it like:
>
>$ perl -Mstrict -Mwarnings -le 'my @arr=3Dqw/aa bb cc/;print =

length(join
"",@arr)' =20
>6
>


Sorry, if you mean the number of elements in an array,then it should be:

my $num =3D scalar @array;

--
Jeff Pang
NetEase AntiSpam Team
http://corp.netease.com

--=20
Perhaps I'm behind the times here, but what's wrong with:

my $num =3D $#array

??

That's how I always do it.

Kim Helliwell
LSI Logic Corporation
Work: 408 433 8475
Cell: 408 832 5365
kim.helliwell@lsi.com
=20
Please Note: My email address will change to kim.helliwell@lsi.com on
Oct 14. The old 'lsil.com' email address will stop working after Jan 15,
2007. Please update your address book and distribution lists
accordingly. Thank you.

To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Jeff Pang

2006-10-12, 6:59 pm


>--
>Perhaps I'm behind the times here, but what's wrong with:
>
>my $num = $#array
>
>??


[coremail@admin ~]$ perl -Mstrict -Mwarnings -le 'my @arr=qw/aa bb cc/;print $#arr'
2
[coremail@admin ~]$ perl -Mstrict -Mwarnings -le 'my @arr=qw/aa bb cc/;print scalar @arr'
3

They are not the same.
The $#arr is the last element's index number,while 'scalar @arr' is all the elements' total number.

--
Jeff Pang
NetEase AntiSpam Team
http://corp.netease.com
Kim Helliwell

2006-10-12, 6:59 pm

-----Original Message-----
From: Jeff Pang [mailto:pangj@earthlink.net]=20
Sent: Monday, October 09, 2006 8:25 AM
To: beginners
Subject: RE: Non-deprecated way to capture array length


>--=20
>Perhaps I'm behind the times here, but what's wrong with:
>
>my $num =3D $#array
>
>??


[coremail@admin ~]$ perl -Mstrict -Mwarnings -le 'my @arr=3Dqw/aa bb
cc/;print $#arr' =20
2
[coremail@admin ~]$ perl -Mstrict -Mwarnings -le 'my @arr=3Dqw/aa bb
cc/;print scalar @arr'
3

They are not the same.
The $#arr is the last element's index number,while 'scalar @arr' is all
the elements' total number.

--
Jeff Pang
NetEase AntiSpam Team
http://corp.netease.com

--=20

Oops, you're right! Guess I'm still half asleep this morning.
But $#arr + 1 is surely another way to get the result sought.
That's the way I always do it, not realizing that scalar @arr
was even available, I guess. However, scalar @arr is computationally
more efficient, I bet, and only slightly longer to write. So it's
probably a better way.

I'm not exactly a Perl beginner, but looks like I learned something
already from this list. Pretty good, seeing I only signed up 3 days
ago....

Kim Helliwell
LSI Logic Corporation
Work: 408 433 8475
Cell: 408 832 5365
kim.helliwell@lsi.com


To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org
<http://learn.perl.org/> <http://learn.perl.org/first-response>


John W. Krahn

2006-10-12, 6:59 pm

Marc Sacks wrote:
> John W. Krahn wrote:
>
>
> Actually, what I did was something like


"what I did was something like" is not the same as "this is the actual code
that produced the warning".


> my @arr = (a,b,c,d,e);
> $length = $arr;


@arr and $arr are two separate variables, the second of which is not even an
array. There is nothing in that example that should produce a warning message.




John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
Uri Guttman

2006-10-12, 6:59 pm

>>>>> "KH" == Kim Helliwell <Kim.Helliwell@lsil.com> writes:

KH> Oops, you're right! Guess I'm still half asleep this morning.
KH> But $#arr + 1 is surely another way to get the result sought.
KH> That's the way I always do it, not realizing that scalar @arr
KH> was even available, I guess. However, scalar @arr is computationally
KH> more efficient, I bet, and only slightly longer to write. So it's
KH> probably a better way.


you rarely need the scalar() call to get an array length. most times you
will be using it in a scalar context like:

$len = @array ;

for my $i ( 1 .. @array ) { ...

using scalar when it isn't needed just adds noise. so learn more about
perl contexts which will help you.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
Paul Lalli

2006-10-12, 6:59 pm

John W. Krahn wrote:

>
> @arr and $arr are two separate variables, the second of which is not even an
> array. There is nothing in that example that should produce a warning message.


where "nothing" == 7, I assume?

$ perl -we'
my @arr = (a, b, c, d, e);
$length = $arr;
'
Unquoted string "a" may clash with future reserved word at -e line 2.
Unquoted string "b" may clash with future reserved word at -e line 2.
Unquoted string "c" may clash with future reserved word at -e line 2.
Unquoted string "d" may clash with future reserved word at -e line 2.
Unquoted string "e" may clash with future reserved word at -e line 2.
Name "main::length" used only once: possible typo at -e line 3.
Name "main::arr" used only once: possible typo at -e line 3.


Paul Lalli

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com