G'day rubystallion,

Welcome to the Monastery.

The approach I took was to read the spec, grab the DATA and write the code. I didn't spend a lot of time looking at your code initially; although, I have commented on it further down in my post. Here's what I came up with:

#!/usr/bin/env perl -l use strict; use warnings; no warnings 'uninitialized'; my @key_value_pairs; # Capture key-value pairs from original query strings while (<DATA>) { chomp; push @key_value_pairs, { map { (split /=/)[0,1] } split /&/ }; } # Remove common key-value pairs KEY: for my $key (keys %{$key_value_pairs[0]}) { for my $i (1 .. $#key_value_pairs) { next KEY unless $key_value_pairs[0]{$key} eq $key_value_pairs[ +$i]{$key}; } delete $key_value_pairs[$_]{$key} for 0 .. $#key_value_pairs; } # Recreate query strings without common key-value pairs for my $kvp (@key_value_pairs) { print join '&', map { join '=', $_, $kvp->{$_} } sort keys %$kvp; } __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

Output:

b=1&c=1&d=2&f=3 b=2&c=3&d=2&f=4 b=2&c=5&d=1&f=5

From the comments embedded in the code, you can see three distinct steps: capture all the initial data; remove the common data; recreate the query strings with what's left.

As you indicated (i.e. "in my head it's very simple") this was fairly straightforward:

  1. split on '&' and then on '='
  2. only delete if all equality tests are TRUE
  3. join with '=' and then with '&'
"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?"

That's a little difficult to answer without knowing what you did on your first three attempts.

A couple of notes on command switches:

And, of course, if anything else in my code needs further explanation, just ask.

-- Ken


In reply to Re: How could I simplify this redundant-column-removing code? by kcott
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.