Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^3: How to add an exceptio for"for loop"

by ELISHEVA (Prior)
on Mar 21, 2011 at 05:05 UTC ( #894408=note: print w/ replies, xml ) Need Help??


in reply to Re^2: How to add an exceptio for"for loop"
in thread How to add an exceptio for"for loop"

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.


Comment on Re^3: How to add an exceptio for"for loop"
Select or Download Code
Re^4: How to add an exceptio for"for loop"
by Anonymous Monk on Mar 21, 2011 at 05:25 UTC

    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) { }

      Insofar as I understand what you're trying to do, it looks OK:

      >perl -wMstrict -le "my @folders = qw(foo .foo foo.bar foo. . .. ...); ;; foreach my $folder (grep { $_ ne '.' } @folders) { printf qq{'$folder' }; } " 'foo' '.foo' 'foo.bar' 'foo.' '..' '...'

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://894408]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-12-20 15:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (96 votes), past polls