Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Hash vs. Hash ref

Reply
Thread Tools

Hash vs. Hash ref

 
 
Rg
Guest
Posts: n/a
 
      05-03-2007
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?

 
Reply With Quote
 
 
 
 
Xicheng Jia
Guest
Posts: n/a
 
      05-03-2007
On May 3, 2:39 pm, Rg <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      05-03-2007
On May 3, 2:39 pm, Rg <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Giving an rvalue ref to a function taking an rvalue ref Juha Nieminen C++ 13 08-29-2012 09:25 PM
passing ref to ptr again as ref to ptr.... osama178@gmail.com C++ 22 04-30-2008 10:42 AM
passing ref to ptr again as ref to ptr.... osama178@gmail.com C++ 6 04-29-2008 08:09 AM
passing ref to ptr again as ref to ptr.... osama178@gmail.com C++ 0 04-24-2008 08:23 PM
strong ref from weak ref? Navindra Umanee Ruby 2 02-12-2005 05:06 PM



Advertisments