lists in perl

Light Poster
12Dec2006,22:07   #1
eanair's Avatar
Is the correct syntax to check if a list is empty in perl:

if(@list == 0)

thanks
Team Leader
12Dec2006,23:40   #2
pradeep's Avatar
If that doesn't work, use this

Code: Perl
if(scalar(@list) == 0)
{
  #empty
}
Go4Expert Member
3Mar2010,13:10   #3
thillai_selvan's Avatar
You can use the following way also.
Code:
my @arr ;
if ( @arr )
{
    print "Array not empty\n";
}
else
{
    print "Array empty\n";
}
There is no need to use the scalar function.
By default this will be treated as scalar context
Go4Expert Member
3Mar2010,13:14   #4
thillai_selvan's Avatar
Another way to test whether a list is empty is as follows

Code:
my @arr ;
if ( $#arr== -1 )
{
    print "Array not empty\n";
}
else
{
    print "Array empty\n";
}
$# will contain the index value of a list.
If a list is not having any value means this will be -1.
If a list is having only one element means this will be 0 and so on.
Team Leader
15Mar2010,17:37   #5
pradeep's Avatar
Code: Perl
unless(@arr)
{
  ## empty
}