Hey there... complex data structures can be fun, quick, and compilcated/frustrating...
In (I think) Advanced Perl Programming there was a piece about dynamic definitions of methods for OOP programming in Perl, and I thought it might be nice here.
Basically instead of defining all the values for a given object, the creation method simply returns a number (starting at 0) and then creates arrays of values for the object, with the names being the values and the element of info for that object in the array offset returned by the creation method (Ill show below)..
Basically they have a defined lang and then the rest of the content is output in the given lang. Therefor $lang will be constant through their instance of the application/webpage/whatever. So you can get the lang definition at the beginning, and retain that for the rest of the script, ALA..
%base_lang = (
'English' => '0',
'French' => '1',
'Japanese' => '2',
);
# then we set up some magic arrays
@red = qw(red french_word_for_red japanese_word_for_red);
@hello = qw(hi bonjour japanese_word_for_hello);
# now the fun
$lang_num = $base_lang{$choice};
print "$red[$lang_num] is $choice for red\n";
print "$hello[$lang_num] is $choice for hello\n";
# so on and so forth
Or even 3 arrays for each of the words in the vocab, ala
# This is a little redundant as it recreates the @red array,
# but @words has all the values so it only dissappears for
# a little bit :)
(@words) = @red;
# Now create out arrays for all the languages in the same
# order to retain our own and our data's sanity :)
foreach $item (@words) {
@{$item} = @words;
}
So for out example with @red (and to save space french_red and japanese_red are the other values);
The above would be similar to doing
@red = qw(red french_red japanese_red);
@french_red = qw(red french_red japanese_red);
@japanese_red = qw(red french_red japanese_red);
Now you have 3 arrays, all for the color red each in the base_lang chosen, with the same values across the board (I.e English is always index 0, French index 1, etc...)
Whats going on is you take an array index (say 0), and then use it consistently across the site/app..(It helps to think of the arrays, as standing on their end (think ladders), with the data you want in the empty space. Then you always run across the ladders sideways, and pull all the right data
If you add another language you can simply add the lang to the base hash, and update all the arrays.
You can even check where you need more information of a given language, by checking to make sure that it has a defined value.
# take for granted all words are within a particular array
# and we recently added German with index of 3
while ( defined($word = shift(@vocab)) ) {
print "No German word for $word!\n" if (!${$word}[3]);
}
The setup may take slightly longer, and in the Advanced Perl Programming book, the author set up a sub to create a new array for a given value if you didn't already exist, but I think I have made the point clear
Hope this helps, and feel free to ask if you have any questions
/*
* And the Creator, against his better judgement, wrote man.c
*/