Re: Non-deprecated way to capture array length

2006-10-09 Thread John W. Krahn
Marc Sacks wrote:
> John W. Krahn wrote:
> 
>> 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.
>
> 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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Non-deprecated way to capture array length

2006-10-09 Thread Helliwell, Kim
-Original Message-
From: Jeff Pang [mailto:[EMAIL PROTECTED] 
Sent: Monday, October 09, 2006 8:25 AM
To: beginners
Subject: RE: Non-deprecated way to capture array length


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

[EMAIL PROTECTED] ~]$ perl -Mstrict -Mwarnings -le 'my @arr=qw/aa bb
cc/;print $#arr'  
2
[EMAIL PROTECTED] ~]$ 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

-- 

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
[EMAIL PROTECTED]


To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




RE: Non-deprecated way to capture array length

2006-10-09 Thread Jeff Pang

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

[EMAIL PROTECTED] ~]$ perl -Mstrict -Mwarnings -le 'my @arr=qw/aa bb cc/;print 
$#arr'  
2
[EMAIL PROTECTED] ~]$ 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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




RE: Non-deprecated way to capture array length

2006-10-09 Thread Helliwell, Kim

-Original Message-
From: Jeff Pang [mailto:[EMAIL PROTECTED] 
Sent: Monday, October 09, 2006 7:48 AM
To: beginners
Subject: Re: Non-deprecated way to capture array length


>
>>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
>

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

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

my $num = $#array

??

That's how I always do it.

Kim Helliwell
LSI Logic Corporation
Work: 408 433 8475
Cell: 408 832 5365
[EMAIL PROTECTED]
 
Please Note: My email address will change to [EMAIL PROTECTED] 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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Re: Non-deprecated way to capture array length

2006-10-09 Thread Jeff Pang

>
>>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
>

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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Non-deprecated way to capture array length

2006-10-09 Thread John W. Krahn
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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Non-deprecated way to capture array length

2006-10-09 Thread Derek B. Smith
--- Marc Sacks <[EMAIL PROTECTED]> 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 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Re: Non-deprecated way to capture array length

2006-10-09 Thread Jeff Pang

>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

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
 




Non-deprecated way to capture array length

2006-10-09 Thread Marc Sacks
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 
[EMAIL PROTECTED]


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 responsible 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 communication in 
error, please notify us immediately by telephone at 617-227-4469 Ext. 228.  
Thank you.



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]