Syntactic Confectionery Delight | |
PerlMonks |
Re: Filtering text file made into hashby AppleFritter (Vicar) |
on Oct 27, 2015 at 10:13 UTC ( [id://1146088]=note: print w/replies, xml ) | Need Help?? |
You're almost there! But as my anonymous brother pointed out, you'll have to think about what you want to achieve and how you want to do it before actually coding it up. You want to print names if they appear at least four times in the file. Just the names, or their associated lengths as well? If the latter, do you want to print all lengths, so long as at least one is greater than or equal to 50? And how often do you want to print them? For instance, take your sample data. Here's a variety of possible outputs I could think of:
I usually go for a general solution that allows me to tweak the output as desired later on without having to change the crunching (much). Which brings me to another important remark - the overall structure for this sort of program is generally:
This may seem trivial, especially since there's no processing going on here (I don't count selecting what to output as processing, really), but it helps to separate these steps. For instance, what kind of data structure is right? You're dealing with key/value pairs here, so it'll likely be something involving a hash. Without knowing exactly what data you need to preserve, I'd suggest simply saving ALL the lengths for each name, in the order they appear in the input file. (As an added benefit, counting the number of lengths for a given name will then also tell you how often that name appeared in your input.) So, use a hash of arrays. perldsc, the Perl Data Structures Cookbook, may be of help there. Further observe that you can use split to break up your input line along commas to separate names from lengths, and use any from the List::Util core module to test if any length for a given name is >= 50, and here's my starting point for a solution:
This outputs:
BTW - I left out the file-handling code on purpose here and read from __DATA__ (see Special Literals for more on that) instead to focus on the important bits. You'll know what to do. :) (In general it's perhaps simpler/better to read from <<>> anyway and use the shell to redirect input and output as desired.)
In Section
Seekers of Perl Wisdom
|
|