in reply to Re^2: syntax of multidimensional arrays and hashes
in thread syntax of multidimensional arrays and hashes
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
|
|---|