in reply to syntax of multidimensional arrays and hashes

"I am converting a hash of arrays to an array of arrays.
I can access the array in the hash as an array. but not its individual elements.
I'm sure that's because I don't understand the syntax."

I agree with that!

foreach $thread (keys %threads) { print "thread=$thread, values are ". @{$threads($thread)}."\n "; }
Past that, I don't understand the context of what you are trying to accomplish. The rest looks like "mumbo-jumbo" to me. Sorry that I don't understand, but I just don't.
$tsize[$tnum] = @{$threads{$thread}};
is nonsense.

Replies are listed 'Best First'.
Re^2: syntax of multidimensional arrays and hashes
by LanX (Saint) on Jul 18, 2009 at 04:58 UTC
    Sorry that I don't understand, but I just don't.
    $tsize[$tnum] = @{$threads{$thread}};
    is nonsense.

    Well syntactically it's correct %threads is a HoA, so @tsize is supposed to hold the sizes of these (now indexedč) arrays.

    I agree that it's hard to imagine why to store the sizes statically...

    Cheers Rolf

    (1) Well should be noted that $tnum is initialized but never incremented... :-)

      The weird part here is that $tsize[$tnum] has pretty much no useful meaning in the code. In a list context, @{$threads{$thread}} has all the values in $threads{$threads}. In a scalar context @{$threads{$thread}} is the number of things in the @{$threads{$thread}} list.

      $tsize[$tnum] = @{$threads{$thread}};
      doesn't appear to have any real meaning or usefulness.

      use @{$threads{$thread}} instead of trying to assign a scalar value to a 'C' style @tsize array.

        I'm going to use @tsize a lot later, eg: to sort all the arrays, so I wanted to access it easily in parallel with the other arrays. And Yes, you've outed me! <G> I'm really a C programmer at heart! It'll take a while before I start thinking in Perl. In any case, $tsize is unrelated to my problem of being unable to retrieve the elements of the array members of the hash.
      Rolf: >should be noted that $tnum is initialized but never incremented< Yes it is - I just cut it off when copying the code to post it this time. It's there in the full code I posted previously.
Re^2: syntax of multidimensional arrays and hashes
by hsfrey (Beadle) on Jul 19, 2009 at 04:33 UTC
    $tsize$tnum = @{$threads{$thread}}; # number of items in the thread Ah, the beauty of Perl! It looks like nonsense, but it works <G> $threads{...} is the hash $thread is the key of the hash $threads{$thread} is the value for that key, which is an array Putting the @ in front of the array returns the size of the array. And, that's is what is correctly printed out. It's one of the FEW things I did correctly! <G>
      VOILA! THIS WORKS!

      $tnum=0; foreach $thread (keys %threads) { $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]; print "\n\t{threads{thread[$i]}}= $threads{$thread}[$i]"; print "\n\tt +list[$tnum][$i]= $tlist[$tnum][$i]"; #dbg } $tnum++; } }
      I had an extra ${...} around the assignment statement in the inner loop! Thanks to all who made suggestions! Harvey