in reply to [untitled node, ID 216639]

I dont really think this is a very scalable solution, it will get confusing and unmaintainable over time.

Chapter 2 in the Advanced Perl Cookbok deals with matricies. To quote 2.2:

@matrix = ( [1, 2, 3], [4, 5, 6], [7, 8, 9] ); # Change 6, the element at row 1, column 2 to 100 $matrix[1][2] = 100;

You can also use a hash representation of the same matrix. (quoting 2.2 again)

$matrix{0}{2} = 100; $matrix{1}{0} = 200; $matrix{2}{1} = 300;

As you dont need to preinit data structures in perl, you can just add a new cell by doing something like: $matrix{1}{0}{0} = 400;

Update: I asked something about variable variables one other time here. It lead me to the conclusion it wasnt really a good idea.. :-)

Replies are listed 'Best First'.
Re: Re: Variables in variable names
by dbp (Pilgrim) on Nov 30, 2002 at 23:43 UTC
    I agree, using a matrix of some sort makes the most sense in this case. Often (actually almost always), if you find yourself using variables to hold other variable names you're looking a perfectly good data structure in the face without realizing it. Variable names like 'a4' scream MATRIX. Here's another way to represent a matrix in perl:
    $matrix{'a,4'} = 20; $matrix{'b,5'} = 30;
    Like the nested hash solution($matrix{'a'}{2} = 5;), it lets you use non-numeric indices but uses a little less memory. You can loop through it like so:
    for my $x ('a'..'g') { for my $y (1..6) { my $val = $matrix{"$x,$y"}; ... } }