The stupid question is the question not asked | |
PerlMonks |
Re: How to make this code more flexibleby Marshall (Canon) |
on Jul 21, 2011 at 22:44 UTC ( [id://916013]=note: print w/replies, xml ) | Need Help?? |
You are doing GREAT for just starting! First, multi-dimensional data structure consists of only references until you get to the last dimension. An ArrayOfArray is what the name says, an array of references to arrays. Earlier I think I saw a 2D structure that you built with a hash for the first dimension. That is not the usual case as you don't get easy sequential processing with a hash. But sometimes this is useful. In that case this is a HashOfArray, or hash of references to arrays. Below is some code that makes both types and returns a reference to each one. There are no limits on the size of the AoA or HoA. I do a split to get @row from the data line. The default split is on 'space',\t\f\r\t so this also gets rid of the end of line (no need for chomp). Then I just push a reference to this row array onto the AoA and to the HoA. One line of code for each one. Whenever your code encounters a "my" variable statement. You are guaranteed to get a "new one". Perl will reuse the space the last one used or if it can't because a reference to it is in existence, new memory will be allocated.
In Section
Seekers of Perl Wisdom
|
|