Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: syntax of multidimensional arrays and hashes

by hsfrey (Beadle)
on Jul 19, 2009 at 03:54 UTC ( [id://781408]=note: print w/replies, xml ) Need Help??


in reply to Re: syntax of multidimensional arrays and hashes
in thread syntax of multidimensional arrays and hashes

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.
  • Comment on Re^2: syntax of multidimensional arrays and hashes

Replies are listed 'Best First'.
Re^3: syntax of multidimensional arrays and hashes
by jethro (Monsignor) on Jul 19, 2009 at 09:44 UTC

    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://781408]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-24 11:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found