in reply to A hash of lists

When you say
$bits{'one'}=@test;
you are really giving @test scalar context. That's why $new[0] the second time returned 3 - that's the number of elements in @test. Basically, you can't just lay these data structures on top of each other.
Try using a reference and dereferencing later:
my @test = ( qw / bing bong bang / ); print '['.$test[2].'] ['.$test[1].'] ['.$test[0]."]\n"; $bits{'one'}= \@test; my @new = @{$bits{'one'}}; print '['.$new[2].'] ['.$new[1].'] ['.$new[0]."]\n"; OUTPUT: [bang] [bong] [bing] [bang] [bong] [bing]
\@test is a reference to test, and  @{$bits{'one'}} accesses the reference ($bits{'one'}) and the @{} dereferences it. Have you tried perlreftut?


UPDATE:
Coruscate reminded me of one of my favorite methods of array reference that I forgot to mention - [@test]. Trust me, all the cool kids are doing it. I hear it's all the rage in Japan. Even though it isn't exactly the same as \@test, for your purposes it works the same.
UPDATE 2:
sauoq has a good point. Chatterbox discussion showed that in fact @{$bits{'one'}} = @test is much faster than [@test] and /@test anyway. Check out my pad for benchmarking details.

Replies are listed 'Best First'.
Re: Re: A hash of lists
by sauoq (Abbot) on Nov 06, 2003 at 05:42 UTC
    Even though it isn't exactly the same as \@test, for your purposes it works the same.

    It's doubtful that his real purposes are anything like the example he showed, so the differences between [ @test ] and \@test may actually be quite significant.

    The first, [ @test ] actually creates a new array, a copy of @test, and returns a reference to it. The second, \@test returns a reference to @test itself.

    If @test is big, making copies of it could hurt performance.

    Also, because the data is copied, changing an element in the array referred to by [ @test ] won't have any affect on the contents of @test itself. Sometimes that's just what you want. Sometimes it isn't.

    -sauoq
    "My two cents aren't worth a dime.";
    
      Ah.. now this is interesting..

      In this instance I plan to read lines from a file and split them by their delimiter and store them as lists in a hash.

      So @test in my example is infact one line read from a file, then @test becomes another line etc..

      So what happens to the data if its references all the way down?

      I suspect I should be using the @test method in this case.

      ___ /\__\ "What is the world coming to?" \/__/ www.wolispace.com
        If you iterate over the file, using @test, then [@test] is indeed your only option, as anything else would have your hash continually referencing the same line (the last one). Hope that helps.