lists in perl

eanair's Avatar, Join Date: Dec 2006
Light Poster
Is the correct syntax to check if a list is empty in perl:

if(@list == 0)

thanks
0
pradeep's Avatar, Join Date: Apr 2005
Team Leader
If that doesn't work, use this

Code: Perl
if(scalar(@list) == 0)
{
  #empty
}
0
thillai_selvan's Avatar, Join Date: Feb 2010
Go4Expert Member
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
0
thillai_selvan's Avatar, Join Date: Feb 2010
Go4Expert Member
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.
0
pradeep's Avatar, Join Date: Apr 2005
Team Leader
Code: Perl
unless(@arr)
{
  ## empty
}