A couple suggestions:
  1. If you are just incrementing by 1, use Foreach Loops instead of C-style loops -- fewer moving parts:
    for my $i (0 .. @F-1) {
  2. 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.
  3. Contrasting the above, you should be using $_ in this case because $line is so highly localized.
    while (<DATA>) { chomp; my @F = split '&';
  4. +@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.
  5. 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.


In reply to Re: How could I simplify this redundant-column-removing code? by kennethk
in thread How could I simplify this redundant-column-removing code? by rubystallion

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.