iphone has asked for the wisdom of the Perl Monks concerning the following question:

Does the following work or is there a better way?

@array = {1,2,3}; #print "ARRAY @array"; if (!@array) { print "ARRAY IS EMPTY\n"; } foreach my $base_p4path (@array)#I want this for loop to get executed +only when @array has elements. { }

Replies are listed 'Best First'.
Re: Is there a better way to check if an array is empty?
by Corion (Patriarch) on Dec 21, 2010 at 08:41 UTC

    So what happened when you tried it?

    Did you try it with an empty array?

    Did you try it with an array that contains one element?

    Did you try it with an array that contains more than one element?

    You might also want to try "interesting" values, like 0, the empty string and undef as array values.

    Did you check your for loop? Does it execute when there are elements in the array? How often does it execute? Does it skip the body if there are no elements in the array?

    What other cases to test for can you think of?

    See also perlsyn.

      If you have one defined element in array then array not empty!
      my $r='undefined'; foreach my $t (@a) { if ($t) {$r='defined';last} } print $r;
Re: Is there a better way to check if an array is empty?
by JavaFan (Canon) on Dec 21, 2010 at 11:41 UTC
    I want this for loop to get executed only when @array has elements.
    Then no check is needed.
    foreach my $base_p4path (@array) { # Execute once for each element in @array. # Hence *NO* loop iteration if @array is empty. }

      I should have been more specific.Basically I want to push $newfile to another array @newfiles only if the array is empty and the instructions after the push instruction should not be executed.The following works for me but I want to know if there is a better way

      if{ my $newfile="$file\n"; @array = grep(/\Q$newfile\E/,keys %hash); if (!@array) { push @newfiles,$newfile; #last; } next unless (@array); ..................... }
        You are using next, and have an outcommented last, without a loop. You also have an if followed by a block.

        I can't make sense of what you are doing, let alone suggest a better way. I do know that many people prefer to use a consistent indenting, but for some reason, many people asking the questions on Perlmonks seem to prefer some random indentation. (But I've seen worse).

        This "code" is gobbledygook. @array is not needed. Grep in a scalar context returns the number of matches.
        my @files = (.....some file names...); my %hash = (....keyed upon file names...); my @newfiles; foreach my $file (@files) { push (@newfiles, $file) unless ( grep{/\Q$file\E/}keys %hash ); # pushes the name of $file to @newfiles if no match # is found to the pattern within keys of %hash. }
Re: Is there a better way to check if an array is empty?
by fisher (Priest) on Dec 21, 2010 at 11:17 UTC
    Try to add/remove some 'pop's' here:
    my @a = ( 1,3,5,7 ); pop @a; pop @a; print "\@a is (" . (join ", ", @a) . ")\n"; unless (@a) { print "empty\n" }