go ahead... be a heretic | |
PerlMonks |
Re: How to improve my code? main concern:array as hash elementby Marshall (Canon) |
on Nov 24, 2011 at 04:41 UTC ( [id://939792]=note: print w/replies, xml ) | Need Help?? |
The problem statement was vague. My interpretation is:
Problem: If my understanding of what you want is wrong, then please correct me and we'll go from there. The best data structure appears to be a HashOfArray (HoA). This eliminates the need for a special case of one term vs more than one term. ++Util Using a HoA in this translation sense is common and is a reasonable approach. I see no need for any kind of regex at all. The right tool here appears to be split, not regex. Check if the number of columns is enough and if so, then check if the term in column 3 can be translated. If both of these are true, then just print one line per translation term. If you want case insensitive comparisons, then convert the translation keys to all one case (upper or lower) and also case the column 3 term the same way.
As a note, using | instead of \t often works much better as a separator because you cannot tell the difference easily between a tab and a space when you look at the file in a normal text editor. And for example, my program editor is set to convert all tabs to spaces. There is no standard for how many spaces a tab should be and formatting gets messed up - so the net of this is that \t separated files are hard to work with.
Update: What this means: The square brackets allocate new anonymous memory for an array (a hunk of memory that has no programmatic predefined name). Each value of %gp is a reference to memory allocated in that way. What Gi => [@gi] does is: allocate new array memory and then copy @gi into it. The hash key, Gi points to that memory. The reference to that memory is a single value and that is why this works in a hash table.
In Section
Seekers of Perl Wisdom
|
|