http://www.perlmonks.org?node_id=1130826

rubystallion has asked for the wisdom of the Perl Monks concerning the following question:

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