Re: Hashes of Hashes



Jason Malburg wrote:
Hi,

Hello,

I'm working on a complex data structure with hashes of hashes and
hashes of arrays and I noticed a weird behavior I was hoping someone
could explain.

Put these two lines at the beginning of your program:

use warnings;
use strict;

and perl will tell you what is wrong.


Here's a sample:

$Data{US17}{1}{GROUP} = "STRING";
$Data{US17}{1}{GROUP}{.001} = 5;
$Data{US17}{1}{GROUP}{.002} = 6;
$Data{US17}{1}{GROUP}{.003} = 7;

The scalar $Data{US17}{1}{GROUP} can only hold either a string or a reference,
not both.

$ perl -le'
use warnings;
use strict;
use Data::Dumper;
my %Data;
$Data{US17}{1}{GROUP} = "STRING";
$Data{US17}{1}{GROUP}{.001} = 5;
$Data{US17}{1}{GROUP}{.002} = 6;
$Data{US17}{1}{GROUP}{.003} = 7;
print Dumper \%Data;
'
Can't use string ("STRING") as a HASH ref while "strict refs" in use at -e line 7.

$ perl -le'
use Data::Dumper;
my %Data;
$Data{US17}{1}{GROUP} = "STRING";
$Data{US17}{1}{GROUP}{.001} = 5;
$Data{US17}{1}{GROUP}{.002} = 6;
$Data{US17}{1}{GROUP}{.003} = 7;
print Dumper \%STRING, \%Data;
'
$VAR1 = {
'0.002' => 6,
'0.003' => 7,
'0.001' => 5
};
$VAR2 = {
'US17' => {
'1' => {
'GROUP' => 'STRING'
}
}
};



John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
.



Relevant Pages

  • Re: debugger exiting
    ... strict and warnings pragmas. ... I think portraying Perl as a command-line tool limits it to fewer platforms than ... work only as a Unix shell command line. ...
    (perl.beginners)
  • Re: dns querry script.
    ... use warnings; ... use strict; ... C:\Dload> perl dns.pl ...
    (comp.lang.perl.misc)
  • Re: Any way to access global variable in Perl script from one module file?
    ... use strict; ... use warnings; ... a separate process has a completely separate memory space. ... There is probably no reason to create a new perl ...
    (comp.lang.perl.misc)
  • Re: Debug Help Please
    ... In Perl there is the expression TIMTOWTDI which means that you will probably get different opinions on "The Right Way" to do something in Perl. ... use strict; ... That is a specious argument because you can disable specific strictures or warnings in local scope: ... for all Failures ...
    (perl.beginners)
  • Re: Align Text
    ... On Dec 21, 2003, at 10:47 PM, Bill Jastram wrote: ... use strict; ... use warnings; ... These promise Perl you'll play by the good programmer rules, ...
    (perl.beginners)