Re: Empty Array

2004-01-02 Thread James Edward Gray II
On Jan 2, 2004, at 10:18 AM, Jeff Westman wrote:

What is the proper way to test if an array is empty ?
Probably the easiest is:

if (@array) {
# not empty
}
else {
# empty
}
# or...

unless (@array) {
# empty
}
This works because an array in scalar context returns the number of 
elements it contains.  If it contains 0, that will equate to false.  
Anything else will be true.

James

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



Empty Array

2004-01-02 Thread Jeff Westman
What is the proper way to test if an array is empty ?


TIA

-JW

__
Do you Yahoo!?
Find out what made the Top Yahoo! Searches of 2003
http://search.yahoo.com/top2003

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




Re: How Do I initialize an empty array

2003-10-29 Thread Tore Aursand
On Tue, 28 Oct 2003 11:53:44 -0700, Wiggins d Anconia wrote:
> or if you know the length of the array ahead of time:
> 
> my @rows = ('','','');

I prefer the following instead:

  my @rows = ('') x 3;

Easier to read...?


-- 
Tore Aursand <[EMAIL PROTECTED]>


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



Re: How Do I initialize an empty array

2003-10-28 Thread Rob Dixon
<[EMAIL PROTECTED]> wrote:
>
> How Do I initialize an empty array so thtat I don't get an unitiialized
> error?
>
> ...
> my @rows;
>
> I thought I could do something like @rows = new (); but it's not working.

You don't.

  my @rows;

declares an array, which is initially empty. If you put
something into it and want it back like it was, then

  @rows = ()

will empty it again.

If you're getting 'Use of uninitialized value' then it's
because you're using an element of the array that hasn't
been assigned (or has been asigned with 'undef').

Rob



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



Re: How Do I initialize an empty array

2003-10-28 Thread perl
thanks. it works.
@rows=();

--
>
>
>> How Do I initialize an empty array so thtat I don't get an unitiialized
>> error?
>>
>> ...
>> my @rows;
>>
>> I thought I could do something like @rows = new (); but it's not
>> working.
>>
>
> Drop the 'new':
>
> my @rows = ();
>
> http://danconia.org
>



-
eMail solutions by 
http://www.swanmail.com

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



Re: How Do I initialize an empty array

2003-10-28 Thread Wiggins d Anconia


> 
> 
> > How Do I initialize an empty array so thtat I don't get an unitiialized
> > error?
> > 
> > ...
> > my @rows;
> > 
> > I thought I could do something like @rows = new (); but it's not
working.
> > 
> 
> Drop the 'new':
> 
> my @rows = ();
> 

Are you sure you are getting the warning on the array or on the elements
of the array you are using?  In which case you can check for
definedness, before using the value, 

perldoc -f defined

or if you know the length of the array ahead of time:

my @rows = ('','','');

For a three element array

http://danconia.org

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



Re: How Do I initialize an empty array

2003-10-28 Thread Wiggins d Anconia


> How Do I initialize an empty array so thtat I don't get an unitiialized
> error?
> 
> ...
> my @rows;
> 
> I thought I could do something like @rows = new (); but it's not working.
> 

Drop the 'new':

my @rows = ();

http://danconia.org

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



How Do I initialize an empty array

2003-10-28 Thread perl
How Do I initialize an empty array so thtat I don't get an unitiialized
error?

...
my @rows;

I thought I could do something like @rows = new (); but it's not working.

thanks


-
eMail solutions by 
http://www.swanmail.com

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



Re: Check for empty array

2002-10-06 Thread Dharmender Rai

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

2002-10-05 Thread John W. Krahn

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

2002-10-05 Thread Michael Fowler

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

2002-10-04 Thread Dharmender Rai

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

2002-10-04 Thread James Edward Gray II

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]




Re: Check for empty array

2002-10-04 Thread Jeremy Vinding

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

2002-10-04 Thread dan

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

2002-10-04 Thread John W. Krahn

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

2002-10-04 Thread James Edward Gray II

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

2002-10-04 Thread Nikola Janceski

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]




Check for empty array

2002-10-04 Thread Bryan Harris



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]