Re: Check for empty array
Yes you are right !! Thanx Dharmender Rai --- John W. Krahn [EMAIL PROTECTED] wrote: Dharmender rai wrote: You can use defined() function by checking for the first element. No, checking the first element of an array will tell you whether or not the first element of the array is defined, it will not tell you if the array is empty. John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] __ Do You Yahoo!? Everything you'll ever need on one web page from News and Sport to Email and Music Charts http://uk.my.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Check for empty array
You can use defined() function by checking for the first element. #!/usr/bin perl use strict; use warnings; my @arr=(); ## empty my $ref=\@arr; ## if(!defined ($ref-[0]) { # do your work here } if (!define ($arr-[0]) { # do your work } --- dan [EMAIL PROTECTED] wrote: or if (!$array[0]) { # it's empty } your choice :) dan Nikola Janceski [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... if(not @array){ # it's empty } -Original Message- From: Bryan Harris [mailto:[EMAIL PROTECTED]] Sent: Friday, October 04, 2002 11:58 AM To: Beginners Perl Subject: Check for empty array What's the easiest way to check whether an array is empty? I'm really feeling like a beginner today... - Bryan -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- -- The views and opinions expressed in this email message are the sender's own, and do not necessarily represent the views and opinions of Summit Systems Inc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] __ Do You Yahoo!? Everything you'll ever need on one web page from News and Sport to Email and Music Charts http://uk.my.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Check for empty array
I just noticed this thread, so forgive me if someone has already mentioned this, or if I'm missing the original point. I just saw some bad examples of how to accomplish what the subject asks, and felt I should chime in. The idiomatic method for checking if an array has elements is simply: if (@array) { ... } If you've stored an array reference, as in $ref = \@array, then dereference: if (@$ref) { ... } See perldoc perldata for help on dealing with data types, perldoc perlref and perldoc perldsc for help on dealing with references. Checking an individual element to see if an array is empty will not work. One of the examples was: if (!defined $ref-[0]) { ... } Given: @array = (undef, foo, bar); $ref = \@array; That test will return a false positive; defined($ref-[0]) will be false, yet there are elements in @array, and by extension, @$ref. Another of the examples was: if (!$array[0]) { ... } Given: @array = (0, foo, bar); That test will also return a false positive; $array[0] is false, yet there are elements in @array. Michael -- Administrator www.shoebox.net Programmer, System Administrator www.gallanttech.com -- -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Check for empty array
Dharmender rai wrote: You can use defined() function by checking for the first element. No, checking the first element of an array will tell you whether or not the first element of the array is defined, it will not tell you if the array is empty. John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Check for empty array
if(not @array){ # it's empty } -Original Message- From: Bryan Harris [mailto:[EMAIL PROTECTED]] Sent: Friday, October 04, 2002 11:58 AM To: Beginners Perl Subject: Check for empty array What's the easiest way to check whether an array is empty? I'm really feeling like a beginner today... - Bryan -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] The views and opinions expressed in this email message are the sender's own, and do not necessarily represent the views and opinions of Summit Systems Inc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Check for empty array
HANDLE_EMPTY() unless @array; James Gray On Friday, October 4, 2002, at 10:57 AM, Bryan Harris wrote: What's the easiest way to check whether an array is empty? I'm really feeling like a beginner today... - Bryan -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Check for empty array
Bryan Harris wrote: What's the easiest way to check whether an array is empty? An array in a scalar context returns the number of elements in the array. if ( @array == 0 ) { # explicit test for zero elements unless ( @array ) { # implicit test for zero elements John -- use Perl; program fulfillment -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Check for empty array
or if (!$array[0]) { # it's empty } your choice :) dan Nikola Janceski [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... if(not @array){ # it's empty } -Original Message- From: Bryan Harris [mailto:[EMAIL PROTECTED]] Sent: Friday, October 04, 2002 11:58 AM To: Beginners Perl Subject: Check for empty array What's the easiest way to check whether an array is empty? I'm really feeling like a beginner today... - Bryan -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- -- The views and opinions expressed in this email message are the sender's own, and do not necessarily represent the views and opinions of Summit Systems Inc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Check for empty array
On Fri, 2002-10-04 at 10:21, [EMAIL PROTECTED] wrote: or if (!$array[0]) { # it's empty } your choice :) dan not quite... try that on this array: @array = qw(0 1 2 3 4 5 6); jjv -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Check for empty array
On Friday, October 4, 2002, at 11:21 AM, dan wrote: or if (!$array[0]) { This tests for a true value in the first element of an array, not if it's empty. This test will succeed for the list (undef, 1, 2, More Data), which is definitely not empty. # it's empty } your choice :) dan Nikola Janceski [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED] ... if(not @array){ # it's empty } -Original Message- From: Bryan Harris [mailto:[EMAIL PROTECTED]] Sent: Friday, October 04, 2002 11:58 AM To: Beginners Perl Subject: Check for empty array What's the easiest way to check whether an array is empty? I'm really feeling like a beginner today... - Bryan -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- -- The views and opinions expressed in this email message are the sender's own, and do not necessarily represent the views and opinions of Summit Systems Inc. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]