Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/forums/perl/)
-   -   lists in perl (http://www.go4expert.com/forums/lists-in-perl-t2218/)

eanair 12Dec2006 22:07

lists in perl
 
Is the correct syntax to check if a list is empty in perl:

if(@list == 0)

thanks

pradeep 12Dec2006 23:40

Re: lists in perl
 
If that doesn't work, use this

Code: Perl

if(scalar(@list) == 0)
{
  #empty
}


thillai_selvan 3Mar2010 13:10

Re: lists in perl
 
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

thillai_selvan 3Mar2010 13:14

Re: lists in perl
 
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.

pradeep 15Mar2010 17:37

Re: lists in perl
 
Code: Perl

unless(@arr)
{
  ## empty
}



All times are GMT +5.5. The time now is 22:04.