I have a few long query strings and to see which parts of the query string matter I wanted to delete those parts which are constant for all query strings. This is equal to removing redundant columns from a '&'-separated table.

I wrote the following code, which seems to work, but I'm not that happy, because in my head it's very simple, like something which should be easily accomplished with a few chained unix commands or a few lines of perl or awk, but what I ended up is relatively long and I only got it to work on the 4th try.

Is there any way to make the code significantly simpler or make it easier for me to write something like this bug-free the first time?

#!/usr/bin/env perl -w use v5.020; my @F1; my @recs; while (my $line = <DATA>) { chomp $line; my @F = split '&', $line; if ($. == 1) { @F1 = @F; } die "NF mismatch" if +@F1 != +@F; push @recs, \@F; for (my $i = 0; $i < @F; $i++) { next unless defined $F1[$i]; if ($F1[$i] ne $F[$i]) { $F1[$i] = undef; } } } for my $rec (@recs) { my $i = 0; for my $field (@$rec) { unless (defined $F1[$i++]) { print "$field\t"; } } say ""; } __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

In reply to 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.