I don't really see the need or usefulness of the first file and array. Personally, on this specific case, I would use two hashes, one for the "+" values and one for the "-" values, because I do not think that a two-level data structure brings very much for this. This is my proposal:
use strict;
use warnings;
my (%plus, %minus);
while (<DATA>) {
my ($fruit, $sign) = split;
if ($sign eq '+') {
$plus{$fruit} = 1;
} else {
$minus{$fruit} = 1;
}
}
print "Positives are : ", (map "$_, ", keys %plus), "\n";
print "Negatives are : ", (map "$_, ", keys %minus), "\n";
__DATA__
apple +
cherry -
cherry +
peach +
banana -
This yields the following output:
$ perl plus_minus.pl
Positives are : peach, cherry, apple,
Negatives are : banana, cherry,
As you can see, real code is about 12 lines or so.
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.