Perl Monk, Perl Meditation | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
Well, how about iterating over one of the arrays by an
index value and pushing that index for each of the arrays
to a data structure?
Not the best way to do it, but it works as long as each array is the same size. <update> In answer to your reply below, Data::Dumper comes with the standard Perl distribution. It is already installed, though it is up to you to check for new versions. Since i was careless to not mention this before (confession: i assumed you knew), i will explain a bit more. When you run this script, you should have the following output:
$VAR1 is not the important part here, don't worry about it for now. What is important is the _value_ of $VAR1, an array reference that contains array referneces - AKA a two dimensional array, which can be called a 'table'. I think this is what you wanted. jwest's solution is possibly better, however. Using his idea, this would be very nice way to do it:
Again, i only use Data::Dumper so that when you run this code, you will see what the datastructure you need to later manipulate will look like, which is this:
Don't worry about the keys (A1,C1, and S1) being out of order (or even in the order that i presented), you can always use sort to order them for you. Try this instead of the printing the Dump of %data:
Now, i would not use that code to insert data into one of my database tables (think quoting issues), but i think it really demonstrates just how powerful Perl is at this task. Ooooo... i just noticed PrakashK's solution. (next day update: i also should be fair and note that even though indapa's solution doesn't use strict (it really doesn't even need to), i like it too.) Why bother saving a key? Using that solution would make my last chunk of code less obfuscated:
Much nicer, but please name your variables more consisely. I notice a lot of PER prefixes in your code, this is a hint that all variables with that prefix might belong together. This symptom is a good candidate for encapsulation, via an object, or simply an array.
jeffa L-LL-L--L-LL-L--L-LL-L-- -R--R-RR-R--R-RR-R--R-RR F--F--F--F--F--F--F--F-- (the triplet paradiddle) In reply to (jeffa) Re: Table building confusion Help Wanted
by jeffa
|
|