Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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", "", etc. will pass the test, i.e. { $_ ne "."} will be true, and therefore will be in @selected.

Replies are listed 'Best First'.
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. . .. ...); ;; foreach my $folder (grep { $_ ne '.' } @folders) { printf qq{'$folder' }; } " 'foo' '.foo' '' 'foo.' '..' '...'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://894408]
[Discipulus]: php does not suck anymore?
[ambrus]: Discipulus: I'm not sure, but it certainly doesn't suck as much as it's used to. it's like C++, it sucks because people still recursively learn from twenty year old PHP examples,
[ambrus]: and they try to use the obsolete features that PHP has to support only for compatibility with old scripts. C++ and PHP both have the problem that people can't forget the past, because when they google "PHP" plus the problme they want to solve, they find b
[ambrus]: ad code examples.
[ambrus]: I'm not trying to recommend PHP, but I think it has way too bad a name because of its past.
[ambrus]: This is different from MS Word, which was already a good editor in the pre-unicode days (in word for windows versions 2 and 6, which ran on windows 3 but also on windows 95), only it wasn't trying to solve the task of writing maths papers back then.
[Discipulus]: ah ok, sounds reasonable; with no fear: Perl all life long
[ambrus]: Mind you, LaTeX is currently still useful for writing math paper or snippet content without styling in such a way that the
[ambrus]: formatting conventions of a journal or website can be quickly applied to it, and MS Office and LibreOffice has not quite solved this (although it's better for this than it used to be),
[ambrus]: which is sort of a drawback compared to the ages of typewritten manuscripts representing content only to which the typesetter applies formatting, but that process required much more manual labor.

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (11)
As of 2017-09-26 11:10 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (293 votes). Check out past polls.