stu96art has asked for the wisdom of the Perl Monks concerning the following question:

Basically, I am wondering if it would be better to use a hash or what I am using, an array that points to other arrays (3d array); I have x,y,z layers. x is the outside layer and has (0..6) values, each one different. The y layer is a list of vertex for the z layer which is a list of blocks. Each block has a different number of vertex, but each vertex has the same number of elements. OK what I am trying to do is put in another vertex in the middle of the list of vertex, yet in doing so I must also add the elements that go with it. I need to know how to put it in and shift those vertex after it. I know that this is the second time that I have looked for advice about this, but I am seriously confused. Here is some things that I have tried, but have not exactly worked out.
OK $datapoint[$kk][3] is just a block number $blocks[][][] is a reference to the array
$t0 = 11111; $t1 = $datapoint[$kk][1]; $t2 = $datapoint[$kk][2]; $t3 = 0; $t4 = 0; $t5 = 99999; $t6 = 1111; for my $rr (($jj + 1)..(($#{$blocks[$datapoint[$kk][3]]}) )) { $temp1 = $blocks[$datapoint[$kk][3]][$rr][0]; $blocks[$datapoint[$kk][3]][$rr][0] = $t0; $t0 = $temp0; } for my $rr (($jj + 1)..(($#{$blocks[$datapoint[$kk][3]]}) )) { $temp1 = $blocks[$datapoint[$kk][3]][$rr][1]; $blocks[$datapoint[$kk][3]][$rr][1] = $t1; $t1 = $temp1; } for my $rr (($jj + 1)..(($#{$blocks[$datapoint[$kk][3]]}) )) { $temp1 = $blocks[$datapoint[$kk][3]][$rr][2]; $blocks[$datapoint[$kk][3]][$rr][2] = $t2; $t2 = $temp2; } for my $rr (($jj + 1)..(($#{$blocks[$datapoint[$kk][3]]}) )) { $temp1 = $blocks[$datapoint[$kk][3]][$rr][3]; $blocks[$datapoint[$kk][3]][$rr][3] = $t3; $t3 = $temp3; } for my $rr (($jj + 1)..(($#{$blocks[$datapoint[$kk][3]]}) )) { $temp1 = $blocks[$datapoint[$kk][3]][$rr][4]; $blocks[$datapoint[$kk][3]][$rr][4] = $t4; $t4 = $temp4; } for my $rr (($jj + 1)..(($#{$blocks[$datapoint[$kk][3]]}) )) { $temp1 = $blocks[$datapoint[$kk][3]][$rr][5]; $blocks[$datapoint[$kk][3]][$rr][5] = $t5; $t5 = $temp5; } for my $rr (($jj + 1)..(($#{$blocks[$datapoint[$kk][3]]}) )) { $temp1 = $blocks[$datapoint[$kk][3]][$rr][6]; $blocks[$datapoint[$kk][3]][$rr][6] = $t6; $t6 = $temp6; } $blocks[$datapoint[$kk][3]][($#{$blocks[$datapoint[$kk][3]]}) + 1][0] += $t0; $blocks[$datapoint[$kk][3]][($#{$blocks[$datapoint[$kk][3]]}) + 1][1] += $t1; $blocks[$datapoint[$kk][3]][($#{$blocks[$datapoint[$kk][3]]}) + 1][2] += $t2; $blocks[$datapoint[$kk][3]][($#{$blocks[$datapoint[$kk][3]]}) + 1][3] += $t3; $blocks[$datapoint[$kk][3]][($#{$blocks[$datapoint[$kk][3]]}) + 1][4] += $t4; $blocks[$datapoint[$kk][3]][($#{$blocks[$datapoint[$kk][3]]}) + 1][5] += $t5; $blocks[$datapoint[$kk][3]][($#{$blocks[$datapoint[$kk][3]]}) + 1][6] += $t6;
And on the second one, I do not know what values I need to put in for the $a and $b in  reverse($a..$b) Should $a be the place that I want to put the new value, and should $b be one less than the last value of the list?
for $i6 ((reverse (($jj + 1))..($#{$blocks[$datapoint[$kk][3]]}) - 1)) + { $blocks[$datapoint[$kk][3]][$i6 + 1][1] = $blocks[ +$datapoint[$kk][3]][$i6][1]; $blocks[$datapoint[$kk][3]][$i6 + 1][2] = $blocks[ +$datapoint[$kk][3]][$i6][2]; $blocks[$datapoint[$kk][3]][$i6 + 1][3] = $blocks[ +$datapoint[$kk][3]][$i6][3]; $blocks[$datapoint[$kk][3]][$i6 + 1][4] = $blocks[ +$datapoint[$kk][3]][$i6][4]; $blocks[$datapoint[$kk][3]][$i6 + 1][5] = $blocks[ +$datapoint[$kk][3]][$i6][5]; $blocks[$datapoint[$kk][3]][$i6 + 1][6] = $blocks[ +$datapoint[$kk][3]][$i6][6]; }

update (broquaint): added <readmore>

Replies are listed 'Best First'.
Re: Hash or not?
by chromatic (Archbishop) on Feb 14, 2003 at 00:12 UTC

    Ouch. Repeated code is usually a sign that you need abstraction or a loop, or a better datastructure. Does this (untested) code replace the first half?

    my @t = ( 11111, $datapoint[$kk][1], $datapoint[$kk][2], 0, 0, 99999, 1111, ); my $point3 = $blocks[ $datapoint[ $kk ][ 3 ] ]; for my $rr ( ($jj + 1) .. $#$point3 ) { (@t, @{ $point3 }[ 0 .. 6 ]) = (@{ $point3 }[ 0 .. 6 ], @t); } push @$point3, \@t;

    Update: demerphq caught a missing square bracket

Re: Hash or not?
by demerphq (Chancellor) on Feb 14, 2003 at 00:50 UTC

    Hi,

    Its no wonder you are confused. The code you are using is extremely clumsy. I say this with no disrespect, but you need to learn to simplify your code. I took everything that you posted and making a few assumptions (is $temp1 supposed to be used each time?) factored it down to

    my $dp=$datapoint[$kk]; my @t=(11111,$dp->[1],$dp->[2],0,0,99999,1111); $blocks_dp3=$blocks[$dp->[3]]; for my $rr ( ($jj + 1)..$#$blocks_dp3 ) { for my $t (0..$#t) { #swap em? ($blocks_dp3->[$rr][$t],$t[$t])=($t[$t],$blocks_dp3->[$rr][$t] +); } } for my $t (0..$#t) { $blocks_dp3[@$blocks_dp3][$t] = $t[$t]; }
    Which to be honest doesnt tell me that much, but at least I can see through the derefrencing and understand what is going on. @bocks appears to be an AoAoA. $rr is an iterator (but what for?), what $jj and $kk are is not clear. @t is a six element list of numbers. Why you want to do the above operations is not at clear, and since the code seems to have basic errors its hard to tell what you are trying to do.

    Since you seem to be working with an AoAoA howabout some code to show you how to walk the whole lot and print them out. You used X,Y,Z so ill use that here

    use strict; use warnings; my $x=[ [ [1..6],[1..6] ], [ [2..7],[2..7] ], [ [3..8],[3..8] ] ]; print "($x) has ".scalar(@$x)." elements\n"; foreach my $ix (0..$#$x) { my $y=$x->[$ix]; print "\t\$x->[$ix] ($y) has ".scalar(@$y)." elements\n"; foreach my $iy (0..$#$y) { my $z=$y->[$iy]; print "\t\t\$x->[$ix][$iy] ($z) has ".scalar(@$z)." elements\n +"; foreach my $iz (0..$#$z) { print "\t\t\t\$x->[$ix][$iy][$iz] = $z->[$iz]\n"; } } }
    In this code its reasonably clear that $ix is the index into @$x, $iy is the index of @$y and so forth. You should be able to use this as a guide to simplying your code and clarifying what you really want to do.

    Also a guide to you is to useData::Dumper; As much as possible. Whenever you are debugging use it. Although for clearly visualizing simpler (non cyclic) data structures you may prefer one of the other dumpers, perhaps Data::PrettySimple or Data::Dump as their output is little easier to read. The ability to see in perl code what your data looks like is a wonderful development and learning tool.

    Anyway, I hope this helps,

    --- demerphq
    my friends call me, usually because I'm late....
    Update: Nice one to chromatic, I started this post and then wandered a way for a while so I didnt see his more elegant simplification. Either way, when two people suggest more or less the same thing its probably worth thinking about :-)