Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Re^2: Deleting array elements

by davorg (Chancellor)
on Jul 23, 2009 at 08:26 UTC ( #782590=note: print w/replies, xml ) Need Help??

in reply to Re: Deleteing array elements
in thread Deleteing array elements

@arr = grep { $_ !~ /c/ } @arr;

That works, of course, with the sample data that we've been given, but in the general case it will remove any element that contains the letter 'c'.

Regular expressions aren't always the answer. Sometimes a simpler approach is right.

@arr = grep { $_ ne 'c' } @arr;

See the Copyright notice on my home node.

Perl training courses

Replies are listed 'Best First'.
Re^3: Deleteing array elements
by si_lence (Deacon) on Jul 23, 2009 at 08:55 UTC
    Hi davorg, you are - of course - right with your remark that regular expressions aren't always the answer.
    It really depends on the problem. If a special named file should be removed from the array then ne is probably the better solution.
    If all files ending in .log should be removed, then a regex might be the way to go.
    Since there is no information about the general problem in the OP we are left guessing.

    cheers, si_lence

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://782590]
[Corion]: Meh. My lack of embedded Pg is showing. I can get the data for an image gallery with one SQL statement, but not with SQLite as it doesn't do window functions :-(
[Corion]: Hmmm. Maybe I can still fake the one spot where I want/need a window function in SQLite... I think I'm never really interested in the real "next" image (not hidden/deleted)

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2018-03-23 17:47 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (295 votes). Check out past polls.