Hello fellow monks, I have the following question.
This example is kind of contrived; I am doing this to test out an algorithm for a larger project.
I have the following in an array:
apple chocolate ice-cream
apple chocolate pie
apple chocolate custard
apple caramel ice-cream
apple caramel pie
apple caramel custard
apple vanilla ice-cream
apple vanilla pie
apple vanilla custard
banana chocolate ice-cream
banana chocolate pie
banana chocolate custard
banana caramel ice-cream
banana caramel pie
banana caramel custard
banana vanilla ice-cream
banana vanilla pie
banana vanilla custard
orange chocolate ice-cream
orange chocolate pie
orange chocolate custard
orange caramel ice-cream
orange caramel pie
orange caramel custard
orange vanilla ice-cream
orange vanilla pie
orange vanilla custard
I have another array with the following:
apple chocolate pie
apple caramel custard
banana caramel ice-cream
orange vanilla pie
What I'm trying to do is remove all items present in the second array, from the first array.
To do that, I have this line of code:
map {@yum = grep {!/$_/} @yum;} @bad;
@yum is the first array and
@bad is the second.
The problem is that when I run this code,
@yum is empty. However, when the equivalent statements produced by the map are run, I get the desired result.
@yum = grep {!/apple chocolate pie/} @yum;
@yum = grep {!/apple caramel custard/} @yum;
@yum = grep {!/banana caramel ice-cream/} @yum;
@yum = grep {!/orange vanilla pie/} @yum;
Why is this happening?
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.