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

Discussion in 'Perl Misc' started by Ronny, Feb 23, 2008.

  1. Ronny

    Ronny Guest

    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
    Ronny, Feb 23, 2008
    #1
    1. Advertising

  2. Ronny

    Guest

    On Feb 23, 8:29 am, Ronny <> 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/
    , Feb 23, 2008
    #2
    1. Advertising

  3. Ronny <> 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/"
    Tad J McClellan, Feb 23, 2008
    #3
  4. Ronny

    Ronny Guest

    On Feb 23, 2:45 pm, Jeremy Nixon <~$!~( )@( )u.defocus.net> wrote:
    > Ronny <> 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;


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

    Ronald
    Ronny, Feb 24, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roy Yao
    Replies:
    4
    Views:
    379
    Roy Yao
    Aug 21, 2003
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,849
    Smokey Grindel
    Dec 2, 2006
  3. thinktwice
    Replies:
    3
    Views:
    336
    Daniel T.
    Feb 10, 2006
  4. Adam Akhtar
    Replies:
    9
    Views:
    167
    Seebs
    Jan 27, 2010
  5. Ronny
    Replies:
    11
    Views:
    320
    comp.llang.perl.moderated
    May 8, 2008
Loading...

Share This Page