in reply to multidimensional arrays

Glaring at your code, I can see (or so I think) what you're trying to achieve, but I'm asking myself whether you're using an appropriate datastructure though.

Multidimensional arrays are fine, as long as you *really* need them (for example, if you're reliant upon indices and order). But let's examine some code first:

$array->[$vi]->[$vj]->[$vk] = $netlist;
If I understand correctly $array->[$vi]->[$vj]->[$vk] resolves to $array->[4]->[3]->[2], agreed? You could be using a 'multidimensional' hash here, because as I understand it, you're not utilising the array indices properly, but rather should have hash keys to lookup items. Should you have the risening need, to have multiple items attached to a key, you could work with a Hash of Hashes of Arrays, for example.

You might find Hash::Flatten appealing as a start.

Furthermore, I don't think, it's worth pointing out minor nits as long as the overall concept doesn't fit in nicely.

Replies are listed 'Best First'.
Re^2: multidimensional arrays
by Anonymous Monk on Jun 10, 2007 at 06:45 UTC
    you understand my code correctly! i used the hash $range just for this question.

    i need to access data by index. The number of indices is a variable.

    array->[4] array->[3]->[5] array->[7]->[2]->[3]
    are all valid.

    interestingly, the problem is CREATING the Ndim array in a general manner. most examples show loops to build each dimension, each nested loop is one dimension. i actually did write this first, and entering the next nested loop was predicated on a valid index. however, i still have to have N nested loops to support N dimensions. i used the "scalar" paradigm for my question as i thought it is more comprehensible.

    i found it straightforward to ACCESS the array generally with the use of "ref". (if the reference is ARRAY, it's another dimension of the array!)

      interestingly, the problem is CREATING the Ndim array in a general manner. most examples show loops to build each dimension, each nested loop is one dimension.

      I don't really think so. It all really depends on how you want to fill it. For example, suppose you just want to create a n-dimensional m x m x ... x m array(-ref) filled with 1's. Then it can be just as simple as in the following example:

      Just as obviously, I wouldn't use anything like that in a real situation, but it was just to convey an idea...

Re^2: multidimensional arrays
by blazar (Canon) on Jun 10, 2007 at 18:12 UTC
    If I understand correctly $array->[$vi]->[$vj]->[$vk] resolves to $array->[4]->[3]->[2], agreed? You could be using a 'multidimensional' hash here, because as I understand it, you're not utilising the array indices properly, but rather should have hash keys to lookup items.

    Well it really depends on how sparse his values are. The fact being that as you correctly hint by quoting, 'multidimensional' hashes are not really, either. One advantage of them over the other approach is that of not incurring in references which could require cloning.