in reply to syntax of multidimensional arrays and hashes

As anon monk already said, turn on warnings. He is wrong on the cause of your problem though, $tsize[$tnum] is (as you expected) the size of the array in the hash of arrays

But in the following line you write $thread[$i] which would try to access an element of the array @thread which simply doesn't exist. What you wanted to do was:
for ($i=0; $i<$tsize[$tnum] ; $i++) { $tlist[$tnum][$i] = $threads{$thread}[$i];

Note that you can eliminate this loop. The following is equivalent to the two lines above

push( @{$tlist[$tnum]}, @{$threads{$thread}} );

Replies are listed 'Best First'.
Re^2: syntax of multidimensional arrays and hashes
by hsfrey (Beadle) on Jul 19, 2009 at 03:54 UTC
    Actually, the element of the array Does exist, as I printed out from the hash in the code above the segment I posted. I tried not to post too much code, since the rest worked. Using $tlist$tnum$i = $threads{$thread}$i; as you suggested, I still got a null instead of a number printed. Then I replaced the loop with the push you recommended, and got the syntax error: "Type of arg 1 to push must be array (not array element)at that line number near "]" " Accordingly I then replaced the $tlist$tnum with the array @tlist, and then got the syntax error "syntax error", without further comment.

      Could there be some mistake in the copying of the code? I tested my code and it works (both versions). Here is my complete sample code. Be sure to use the download button underneath before you copy/paste the code so you don't copy word wrapped lines. And remember that for the code with the push you also need to remove the closing '}' of the loop.

      #!/usr/bin/perl use warnings; %threads=('a', [1,2,3], 'b', [2,2,3], 'e', [3,2,3], 'd', [4,2,3], 'c', [5,2,3]); $tnum=0; foreach $thread (keys %threads) { print "\n thread for $thread, tnum=$tnum:"; #dbg print "\n\tthe array from hash=@{$threads{$thread}}"; #dbg # THE ABOVE LINE CORRECTLY PRINTS THE ELEMENTS OF THE ARRAY OF THE HAS $tsub[$tnum] = $thread; $tsize[$tnum] = @{$threads{$thread}}; print "\n\ttsize[tnum]= $tsize[$tnum]"; #dbg # for ($i=0; $i<$tsize[$tnum] ; $i++) # { $tlist[$tnum][$i] = $threads{$thread}[$i]; push( @{$tlist[$tnum]}, @{$threads{$thread}} ); # HERE I'M TRYING TO PRINT THE ELEMENTS ONE AT A TIME print "\n\t\@{threads{thread}}=@{$threads{$thread}}"; # BUT THE ABOVE LINE PRINTS ONLY A NULL }

      which gives me the following output (note that as LanX found out you don't increment tnum, so there is still a bug in this code):

      Name "main::tsub" used only once: possible typo at ./t7.pl line 17. Name "main::tlist" used only once: possible typo at ./t7.pl line 22. thread for e, tnum=0: the array from hash=3 2 3 tsize[tnum]= 3 {threads{thread[0]}}=3 2 3 thread for c, tnum=0: the array from hash=5 2 3 tsize[tnum]= 3 {threads{thread[0]}}=5 2 3 thread for a, tnum=0: the array from hash=1 2 3 tsize[tnum]= 3 {threads{thread[0]}}=1 2 3 thread for b, tnum=0: the array from hash=2 2 3 tsize[tnum]= 3 {threads{thread[0]}}=2 2 3 thread for d, tnum=0: the array from hash=4 2 3 tsize[tnum]= 3 {threads{thread[0]}}=4 2 3