Clear questions and runnable code get the best and fastest answer |
|
PerlMonks |
Calculating percentage of change between columnsby naChoZ (Curate) |
on May 29, 2013 at 22:47 UTC ( [id://1035927]=perlquestion: print w/replies, xml ) | Need Help?? |
naChoZ has asked for the wisdom of the Perl Monks concerning the following question: I'm wondering if there's a simple way I could shoehorn this feature in without a full rewrite. Basically I want to just throw in a quick calculation of changes between columns. Here's a trimmed down example of what I'm doing: Read a file of num to name mappings:
Read any number of files (granted, it's rarely more than a handful) that look like this:
I iterate and increment values in a hashref as I go so it ends up looking like:
And then simply I produce a report like this to make it easy to spot the differences:
What I'd like to do is something like this:
My code is very straightforward, the only difference being I'm handling a file with 20+ columns and the each file is 300,000+ lines. I'm populating %$hash exactly as you might expect, opening each file, iterating, if $hash->{$file}->{$name} isn't defined I define it, otherwise I increment $hash->{$file}->{$name}++. (The other subtle differences are intentional, like how 99 appears on some of the rows, but since it doesn't appear in the mapping of nums to names, I don't include it in the report.) It doesn't seem like trying to calculate differences inside the same loop I'm using to iterate the files is the way to go. I only see a couple of possible paths, but I'm can't wrap my head around either of them very well. Should I iterate the resulting %$hash after I finish creating it and make a new hash out of the results? Or, while I populate %$hash, should I also somehow populate an additional hash to make it easy to calculate later? Any advice appreciated. Just so to include the obligatory code sample it goes something like this: Then I iterate @heading and @report and print them out cell by cell. Any tips on how I might add the percentage change between columns?
--
Back to
Seekers of Perl Wisdom
|
|