Pathologically Eclectic Rubbish Lister | |
PerlMonks |
Specifying a loop with varying number of keysby periapt (Hermit) |
on Mar 22, 2005 at 14:20 UTC ( [id://441464]=perlquestion: print w/replies, xml ) | Need Help?? |
periapt has asked for the wisdom of the Perl Monks concerning the following question:
OK, I'm looking for an alternative implementation that allows me to categorize a data element based on 2 or maybe 3 keys. Here is what I'm working on. I am importing several pipe seperated text files into a database. Record length varies by file but is constant within a file. To simplify processing, I am parsing each record into individual data elements based on a set of four key values. Two are static to a record and two are dynamic. Assume the dynamic keys are sequential so that key1 varies, say, from 1 to 3 and key2 varies from 1 to 6. The range of the key values depend on the data file I'm processing at the time. It turns out that I can process most of the data files with the same algorithm. I just have to set a couple of initialization parameters. The one monkey wrench is that one of the files requires three key values. It is also possible that future data added to this system will also require three key values. I've come up with a rather inelegant partial solution that still allows me to use one algorithm but I'm wondering if there isn't something better. something with references? aliases? fancy symbol table manipulation? function templates? As you can see from the below code, the sticking point is the insert statment which is incorrectly specified if a third key is not needed. I would like to find some way to specify the @key array so that it contains the correct number of elements without resorting to a ladder of if..elsif 's (or some similar structure). The idea is to keep the code as compact and generic as possible since it is certain that I will be adding data sets in the future which may also require a differing number of key values. Super search and google search didn't turn up much of use (lot's of almost but not quite). Any suggestions?
PJ use strict; use warnings; use diagnostics;
Back to
Seekers of Perl Wisdom
|
|