references

Discussion in 'Perl' started by toylet, Feb 19, 2004.

  1. toylet

    toylet Guest

    I am studying the examples in the book "Perl 5 by Examples". Here's a
    short program from that book:

    my $dbf2 = { 3 => { 4=>5, 5=>6 } };
    my($key1,$value1,$key2,$value2);
    while (($key1,$value1) = each(%{$dbf2})) {
    print("level 1: $key1 => $value1\n");
    while(($key2,$value2) = each(%{$value1})) {
    print("level 2: $key2 => $value2\n");
    }
    }

    The program would prints:

    Level 1: 3 => HASH(123456)
    Level 2: 4 => 5
    Level 2: 5 => 6

    Now, what if I create this (which passed the compilation):

    my $dbf3 = { { 4=>5, 5=>6} }

    How could I make the same program work?

    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 4:54pm up 2 days 18:48 load average: 1.00 1.00 1.00
    toylet, Feb 19, 2004
    #1
    1. Advertising

  2. toylet wrote:
    > I am studying the examples in the book "Perl 5 by Examples". Here's a
    > short program from that book:
    >
    > my $dbf2 = { 3 => { 4=>5, 5=>6 } };
    > my($key1,$value1,$key2,$value2);
    > while (($key1,$value1) = each(%{$dbf2})) {
    > print("level 1: $key1 => $value1\n");
    > while(($key2,$value2) = each(%{$value1})) {
    > print("level 2: $key2 => $value2\n");
    > }
    > }
    >
    > The program would prints:
    >
    > Level 1: 3 => HASH(123456)
    > Level 2: 4 => 5
    > Level 2: 5 => 6
    >
    > Now, what if I create this (which passed the compilation):
    >
    > my $dbf3 = { { 4=>5, 5=>6} }
    >
    > How could I make the same program work?
    >

    What are you trying to accomplish? In $dbf2 you have a hash that has
    one key/value pair. Key name "3" that has a value that is another hash
    has 2 key/value pairs. When you defined $dbf3 you have defined a hash
    that has a single key/value pair the key is a hash and the value is
    undefined.

    To better see what data structures you have created try using
    Data::Dumper module.
    Here is what Dumper gives you for you two hashes:
    (This is $dbf2)
    $VAR1 = {
    '3' => {
    '4' => 5,
    '5' => 6
    }
    };

    (This is $dbf3)
    $VAR1 = {
    'HASH(0x8260d54)' => undef
    };




    --
    Thanks
    Charles LaCour
    Charles LaCour, Feb 20, 2004
    #2
    1. Advertising

  3. toylet

    toylet Guest

    thank you for the tip on Data::Dumper. I was just testing my skills on
    accessing data structures of Perl.

    > To better see what data structures you have created try using
    > Data::Dumper module.
    > Here is what Dumper gives you for you two hashes:
    > (This is $dbf3)
    > $VAR1 = {
    > 'HASH(0x8260d54)' => undef
    > };


    --
    .~. Might, Courage, Vision. In Linux We Trust.
    / v \ http://www.linux-sxs.org
    /( _ )\ Linux 2.4.22-xfs
    ^ ^ 10:50am up 23 min 0 users 1.12 1.07
    toylet, Feb 20, 2004
    #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. Roger Leigh
    Replies:
    8
    Views:
    434
    Karl Heinz Buchegger
    Nov 17, 2003
  2. Replies:
    3
    Views:
    447
    Victor Bazarov
    Nov 10, 2004
  3. DanielEKFA
    Replies:
    8
    Views:
    601
    DanielEKFA
    May 16, 2005
  4. Replies:
    8
    Views:
    710
    Bruno Desthuilliers
    Dec 12, 2006
  5. Lars Willich
    Replies:
    13
    Views:
    833
    Ian Shef
    Oct 23, 2007
Loading...

Share This Page