in reply to Re: A hash of lists
in thread A hash of lists

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.";

Replies are listed 'Best First'.
Re: Re: Re: A hash of lists
by wolis (Scribe) on Nov 06, 2003 at 22:42 UTC
    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.
        Depends on if my is used, doesn't it?

        I went to check, and wrote this script:

        #!/your/perl/here use strict; use warnings; use Data::Dumper; my @array = 0..4; print "\@array ", \@array, "\n"; print Dumper @array; my %hash; $hash{one} = \@array; print "\%hash x1\n"; print Dumper %hash; my @array = 'a'..'e'; print "\@array ", \@array, "\n"; print Dumper @array; $hash{two} = \@array; print "\%hash x2\n"; print Dumper %hash; print "";
        Which gives this output:
        c:\perl\perl>hash_array.pl "my" variable @array masks earlier declaration in same scope at C:\perl\perl\hash_array.pl line 16. @array ARRAY(0x1a452dc) $VAR1 = 0; $VAR2 = 1; $VAR3 = 2; $VAR4 = 3; $VAR5 = 4; %hash x1 $VAR1 = 'one'; $VAR2 = [ 0, 1, 2, 3, 4 ]; @array ARRAY(0x1ae73c8) $VAR1 = 'a'; $VAR2 = 'b'; $VAR3 = 'c'; $VAR4 = 'd'; $VAR5 = 'e'; %hash x2 $VAR1 = 'one'; $VAR2 = [ 0, 1, 2, 3, 4 ]; $VAR3 = 'two'; $VAR4 = [ 'a', 'b', 'c', 'd', 'e' ];
        which differs from the debug x output at the end:
        c:\perl\perl>perl -d hash_array.pl Loading DB routines from perl5db.pl version 1.19 Editor support available. Enter h or `h h' for help, or `perldoc perldebug' for more help. main::(hash_array.pl:7): my @array = 0..4; DB<1> c 24 [snip Dumper output] main::(hash_array.pl:24): print ""; DB<2> x \%hash 0 HASH(0x1c23ca4) 'one' => ARRAY(0x1c1b9ac) 0 0 1 1 2 2 3 3 4 4 'two' => ARRAY(0x1c1b9ac) -> REUSED_ADDRESS
        But if I enclose the last my @array... in a block, the debugger gives the same answer (and the compiler warning goes away).

        Any hints?

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of