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

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

Replies are listed 'Best First'.
Re: Re: Re: Re: A hash of lists
by jweed (Chaplain) on Nov 06, 2003 at 23:09 UTC
    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