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

I am trying to insert a list of values into a 3D array, not substituting them, but inserting them between @blocks[$i8][$it] and @blocks[$i8][$it + 1]. This will increase the total # of vertex by one, shifting all of the vertex after @blocks[$i8][$it]. Can someone please help me with this, thanks. Here is some of the code I am trying to use. I have a 3D array @blocks. It looks like this $blocks[$i][$j][$k]. $i is a list of blocks. $j is a list of vertex that make up the block. and $k is 0-6 where 0= # of the point 1= x-value of the point 2= y-value of the point and 3 through 6 are other values of the point. What I am trying to do is insert all of the information of a point, thus creating a new one and shifting the list of vertex by 1.
# $it2 is where I want to put the new vertex info "@t" # I want to put the new info in between @blocks[$i8][$it] # and @blocks[$i8][$it + 1] # $i8 is the block that I want to insert the new vertex # information into # $datapoint[$kk][1] is the new x-value # $datapoint[$kk][2] is the new y-value my @t = ( 11111, $datapoint[$kk][1], $datapoint[$kk][2], 0, 0, 99999, 11111, ); splice (@{blocks[$i8]}, ($it2 + 1), 0, \@t);

Replies are listed 'Best First'.
Re: inserting a list into an array
by BrowserUk (Patriarch) on Apr 23, 2003 at 01:15 UTC

    I can only echo the bemusment of the others in saying that your call to splice seems to be coded correctly and that the problem must lie with the code you aren't showing us.

    #! perl -slw use strict; my @blocks = ( [ ['A1',1,2,3,4,5,6], ['B1',1,2,3,4,5,6], ['C1',1,2,3,4,5,6], ['D +1',1,2,3,4,5,6], ], [ ['A2',1,2,3,4,5,6], ['B2',1,2,3,4,5,6], ['C2',1,2,3,4,5,6], ['D +2',1,2,3,4,5,6], ], [ ['A3',1,2,3,4,5,6], ['B3',1,2,3,4,5,6], ['C3',1,2,3,4,5,6], ['D +3',1,2,3,4,5,6], ], [ ['A4',1,2,3,4,5,6], ['B4',1,2,3,4,5,6], ['C4',1,2,3,4,5,6], ['D +4',1,2,3,4,5,6], ], [ ['A5',1,2,3,4,5,6], ['B5',1,2,3,4,5,6], ['C5',1,2,3,4,5,6], ['D +5',1,2,3,4,5,6], ], ); my @new_vertex = ('N1',1,2,3,4,5,6); my $nv_blockn = 2; my $nv_pos = 2; splice @{ $blocks[$nv_blockn] }, $nv_pos, 0, \@new_vertex ; { local $" =','; #" print qq[[ @{[ map{ "[@$_]" } @$_ ]} ],] for @blocks; } __END__ C:\test>252393.pl [ [A1,1,2,3,4,5,6],[B1,1,2,3,4,5,6],[C1,1,2,3,4,5,6],[D1,1,2,3,4,5,6] +], [ [A2,1,2,3,4,5,6],[B2,1,2,3,4,5,6],[C2,1,2,3,4,5,6],[D2,1,2,3,4,5,6] +], [ [A3,1,2,3,4,5,6],[B3,1,2,3,4,5,6],[N1,1,2,3,4,5,6],[C3,1,2,3,4,5,6], +[D3,1,2,3,4,5,6] ], [ [A4,1,2,3,4,5,6],[B4,1,2,3,4,5,6],[C4,1,2,3,4,5,6],[D4,1,2,3,4,5,6] +], [ [A5,1,2,3,4,5,6],[B5,1,2,3,4,5,6],[C5,1,2,3,4,5,6],[D5,1,2,3,4,5,6] +],

    When I'm trying to track down bugs and logic errors that I simply cannot seem to find, I often resort to writing a new script that has just the minimum required to demonstrate the effect I am trying to acheive. And I really do tend to code this from scratch rather than trying to cut down the original to its minimum, as this often clarifies the problem in my logic as I do so. With the bonus that if my logic is wrong, I can play with the small script until I get it right, and if I can't get it to work, or if I really have found a bug, it serves as a self contained demonstration when requesting help or reporting the bug.

    The small sample above took just a few minutes to generate...actually less time than typing this post:). Maybe it will clarify something for you.


    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
Re: inserting a list into an array
by Enlil (Parson) on Apr 22, 2003 at 23:43 UTC
    I don't understand where you are trying to insert because of the text says one thing, and your comment in your code says something else.:
    • Text: between @blocks[$i8][$it] and @blocks[$i8][$it + 1]
    • Comment: # $it2 is where I want to put the new vertex info "@t"

    but the results that I am getting is that the valuse are added to the right block, but at the end of the list of vertex, instead of into the right position and the rest shifted.

    Do you have warnings enabled (either using -w or use warnings; )? It might be that $it2 + 1 is equal to the length of the array or more than that. If it is offset more than the length of the array, perl will issue a warning and then splices at the end of the array. e.g.,

    -enlil

Re: inserting a list into an array
by Abigail-II (Bishop) on Apr 22, 2003 at 23:25 UTC
    The splice looks correct to me, but what does seem to be confusing is that comment says you want to insert between $it and $it + 1, but the code uses $it2.

    Abigail

Re: inserting a list into an array
by stu96art (Scribe) on Apr 22, 2003 at 23:19 UTC
    Sorry I did not add this to my first submission, but the results that I am getting is that the valuse are added to the right block, but at the end of the list of vertex, instead of into the right position and the rest shifted. Thnaks for your help.
Re: inserting a list into an array
by stu96art (Scribe) on Apr 23, 2003 at 00:40 UTC
    I apologize, $it = $it2 They are the same thing. I do not understand why it is inserting it at the end of the list?
      What is the value of $it before you do the splice? And what it in the big array at that time?

      Abigail