No such thing as a small change | |
PerlMonks |
Re: How could I simplify this redundant-column-removing code?by aaron_baugher (Curate) |
on Jun 17, 2015 at 18:07 UTC ( [id://1130851]=note: print w/replies, xml ) | Need Help?? |
This is kind of an interesting problem. If I were being handed this task, my first two questions would be:
For a real-world task, I'd assume both answers are 'no,' so I'd have to be prepared to handle missing keys (including keys not appearing in the first line), and keys out of order. Given all that, I'd store the keys and values of each line in an array of hashes (an array to maintain the order of the lines). I'd also have a hash for keeping track of all the keys, and another hash for tracking which keys have the same value throughout so they can be dropped from the output. (There may be a clever way to make one hash do both those things, but it didn't occur to me.) This is what I ended up with. It drops the key/value pairs that are always the same (a & e), regardless of order or whether a key is sometimes missing. One note: I tell the inner split to always produce 2 fields, because otherwise it'll produce undef where there's no value, which messes up the nifty map-to-hash.
Aaron B.
In Section
Seekers of Perl Wisdom
|
|