in reply to How to add an exceptio for"for loop"

perl -e 'my @a=qw|f1 fw . f3 f4|; for (grep {$_ ne "."} @a) {print qq| +$_\n|}'

     Syntactic sugar causes cancer of the semicolon.        --Alan Perlis

Replies are listed 'Best First'.
Re^2: How to add an exceptio for"for loop"
by Anonymous Monk on Mar 21, 2011 at 04:55 UTC

    grep will not work because other array elements also have a "." in them.I dont want a loop on the array element which is exactly "."

      I think you may be confusing the command line grep with Perl's grep command. my @selected = grep { $_ ne "." } @some_array selects elements based on whether the content of {....} returns true or false. As { $_ ne "."} is only true on an exact non-match with ".", it will exclude only ".".

      ".foobar", "foo.bar", etc. will pass the test, i.e. { $_ ne "."} will be true, and therefore will be in @selected.

        Do you spot anything wrong in the below line,for some reason the for loop is executed even for the element "." in @folders

        foreach my $folder (grep {$_ ne "."}@folders) { }