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:
"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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |