For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > February 2008 > Storing references in a hash - why does Perl complain here?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Storing references in a hash - why does Perl complain here?
Ronny

2008-02-23, 8:06 am

I would like to build a hash, where the keys are strings, and the
values are
references (actually, they are references to anonymous hashes). When I
start like this:

my %uts={};
$uts{X}={};

Perl 5.8.6 complains:

"Reference found where even-sized list expected"

Now I don't question the *content* of this sentence (actually, {} *is*
a reference), but I wonder
why Perl insists in having something like

$uts{X}=();

instead. What is wrong in storing a hash reference in a hash? Other
references (I tried references
to numbers, references to arrays, and code references) work fine.

Ronald
Jeremy Nixon

2008-02-23, 8:06 am

Ronny <ro.naldfi.scher@gmail.com> wrote:

> my %uts={};
> $uts{X}={};
>
> Perl 5.8.6 complains:
>
> "Reference found where even-sized list expected"
>
> Now I don't question the *content* of this sentence (actually, {} *is*
> a reference), but I wonder
> why Perl insists in having something like
>
> $uts{X}=();
>
> instead.


It's not complaining about that, it's complaining about the line before it,
which should be

my %uts = ();

Or simply

my %uts;

%uts is a hash, not a hash reference; {} is an empty hash reference, which
would be assigned to a scalar variable, not a hash.

--
Jeremy Nixon | address in header is valid
(formerly jeremy@exit109.com)
1usa@llenroc.ude.invalid

2008-02-23, 8:06 am

On Feb 23, 8:29 am, Ronny <ro.naldfi.sc...@gmail.com> wrote:
> I would like to build a hash, where the keys are strings, and the
> values are
> references (actually, they are references to anonymous hashes). When I
> start like this:
>
> my %uts={};
> $uts{X}={};
>
> Perl 5.8.6 complains:
>
> "Reference found where even-sized list expected"


That is not the whole error message. It is important to read all that
perl tells you: It should have given you a line number.

perl is complaining about

> my %uts={};


That should have been:

my %uts = ();

But, then, you really do not need the initializer, so

my %uts;

would have been better.

Sinan

--
Cool & cute: http://eeepc.asus.com/global/
Tad J McClellan

2008-02-23, 7:08 pm

Ronny <ro.naldfi.scher@gmail.com> wrote:

> I would like to build a hash, where the keys are strings,



Hash keys are *always* strings.


> and the
> values are
> references (actually, they are references to anonymous hashes). When I
> start like this:
>
> my %uts={};
> $uts{X}={};
>
> Perl 5.8.6 complains:
>
> "Reference found where even-sized list expected"



Did the complaint include a line number?

Did you pay close attention to which line the message was
referring to?

It is complaining about the 1st line, not the 2nd one.


> Now I don't question the *content* of this sentence (actually, {} *is*
> a reference), but I wonder
> why Perl insists in having something like
>
> $uts{X}=();
>
> instead.



That is not what it is insisting on at all, rather it is insisting
on something like:

my %uts=()
or
my %uts;

instead.


> What is wrong in storing a hash reference in a hash?



Nothing.

What is wrong with storing a hash reference as a hash _key_?

Is a question that applies to your code...


> Other
> references (I tried references
> to numbers, references to arrays, and code references) work fine.



If you had shown us your code using those other types of references,
then we could have explained how and why they are different.

But you didn't, so we can't.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
Ronny

2008-02-24, 8:13 am

On Feb 23, 2:45 pm, Jeremy Nixon <~$!~( )@( )u.defocus.net> wrote:
> Ronny <ro.naldfi.sc...@gmail.com> wrote:
>
>
>
>
>
>
> It's not complaining about that, it's complaining about the line before it,
> which should be
>
> my %uts = ();
>
> Or simply
>
> my %uts;


Of course, you are right!!!! Thank you for pointing this out!

Ronald
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com