Hash vs. Hash ref

Discussion in 'Perl Misc' started by Rg, May 3, 2007.

  1. Rg

    Rg Guest

    Saluton,

    Please, take a look at the following Perl program:

    -----------------------
    #!/usr/bin/perl

    use strict;

    my $hash_ref = {
    List => ()
    };

    $hash_ref->{List}[0]->{Something} = "Boo";
    $hash_ref->{List}[1]->{Something_else} = "Not boo";

    print "Something is $hash_ref->{List}[0]{Something}\n";
    print "Something else is $hash_ref->{List}[1]{Something_else}\n";
    -----------------------

    The output for this program is

    "Something is Boo
    Something else is Not boo"

    I wonder why. How comes Perl treat $hash_ref->{List}[0] both as a hash
    reference and a hash? Am I missing something here? Is there "undefined
    behavior" involved?
     
    Rg, May 3, 2007
    #1
    1. Advertising

  2. Rg

    Xicheng Jia Guest

    On May 3, 2:39 pm, Rg <> wrote:
    > Saluton,
    >
    > Please, take a look at the following Perl program:
    >
    > -----------------------
    > #!/usr/bin/perl
    >
    > use strict;
    >
    > my $hash_ref = {
    > List => ()


    ITYM:
    List => [ ],

    > };
    >
    > $hash_ref->{List}[0]->{Something} = "Boo";
    > $hash_ref->{List}[1]->{Something_else} = "Not boo";
    >
    > print "Something is $hash_ref->{List}[0]{Something}\n";
    > print "Something else is $hash_ref->{List}[1]{Something_else}\n";
    > -----------------------
    >
    > The output for this program is
    >
    > "Something is Boo
    > Something else is Not boo"
    >
    > I wonder why. How comes Perl treat $hash_ref->{List}[0] both as a hash
    > reference and a hash? Am I missing something here? Is there "undefined
    > behavior" involved?


    no, they are both references, check what's the difference between:

    $hash_ref->{List}[0]{something}
    $hash_ref->{List}[0]->{something}

    Read "Intermediate Perl" section 4.7

    Regards,
    Xicheng
     
    Xicheng Jia, May 3, 2007
    #2
    1. Advertising

  3. Rg

    Paul Lalli Guest

    On May 3, 2:39 pm, Rg <> wrote:
    > Saluton,
    >
    > Please, take a look at the following Perl program:
    >
    > -----------------------
    > #!/usr/bin/perl
    >
    > use strict;
    >
    > my $hash_ref = {
    > List => ()


    This doesn't do what you think it does. ALWAYS enable warnings when
    writing Perl code!!

    >
    > };
    >
    > $hash_ref->{List}[0]->{Something} = "Boo";
    > $hash_ref->{List}[1]->{Something_else} = "Not boo";
    >
    > print "Something is $hash_ref->{List}[0]{Something}\n";
    > print "Something else is $hash_ref->{List}[1]{Something_else}\n";
    > -----------------------
    >
    > The output for this program is
    >
    > "Something is Boo
    > Something else is Not boo"
    >
    > I wonder why. How comes Perl treat $hash_ref->{List}[0] both as a hash
    > reference and a hash?


    What gives you the impression anything in the above is indicating
    that? The second arrow and lack thereof?

    > Am I missing something here?


    Yes. The "arrow rule". You should read up on how multidimensional
    structures are accessed in Perl. Take a look at :
    perldoc perlreftut
    perldoc perllol
    perldoc perldsc.

    > Is there "undefined behavior" involved?


    Not at all. It's perfectly well defined. It's your assumption that
    is in error.

    Paul Lalli
     
    Paul Lalli, May 3, 2007
    #3
    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. Replies:
    0
    Views:
    349
  2. Replies:
    22
    Views:
    775
    peter koch
    Apr 30, 2008
  3. Replies:
    6
    Views:
    359
    James Kanze
    Apr 29, 2008
  4. Navindra Umanee

    strong ref from weak ref?

    Navindra Umanee, Feb 12, 2005, in forum: Ruby
    Replies:
    2
    Views:
    147
    Navindra Umanee
    Feb 12, 2005
  5. Justin C
    Replies:
    1
    Views:
    195
    Justin C
    Oct 7, 2013
Loading...

Share This Page