A couple suggestions:
- If you are just incrementing by 1, use Foreach Loops instead of C-style loops -- fewer moving parts:
for my $i (0 .. @F-1) {
- Avoid punctuation variables if you can, unless this is your toy and you are super comfy with them. Rather than testing $., just test if @F1 is initialized:
@F1 = @F if !@F1;
Note that you shouldn't use logical compound assignment operators because they have scalar context.
- Contrasting the above, you should be using $_ in this case because $line is so highly localized.
while (<DATA>) {
chomp;
my @F = split '&';
- +@F1 is a no-op. Numification requires two arguments, so you'd need to write 0+@F1, but you don't even need to do that because logical operators like != also apply scalar context to their arguments.
- Your algorithm gets simpler and allows using a hash if you track which terms to delete rather than which ones to keep. I'm assuming that you don't have repeated keys.
So I might write that as:
#!/usr/bin/env perl -w
use v5.014;
my %seen;
my $count;
my @recs;
while (<DATA>) {
chomp;
my @F = split '&';
$count //= @F;
die "NF mismatch" if @F != $count;
$seen{$_}++ for @F;
push @recs, \@F;
}
for my $rec (@recs) {
say join "\t",
grep $seen{$_} != @recs, # Doesn't show up in every record
@$rec
;
}
__DATA__
a=1&b=1&c=1&d=2&e=&f=3
a=1&b=2&c=3&d=2&e=&f=4
a=1&b=2&c=5&d=1&e=&f=5
#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.
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.