Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Regarding Initialization and Use of Hashes

by Zaemz (Initiate)
on Sep 04, 2012 at 16:56 UTC ( #991655=perlquestion: print w/ replies, xml ) Need Help??
Zaemz has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have a question regarding the use of hashes. I'm reading some tutorials a the moment and it uses the following as an example of a hash table variable:

my $address = { Name => 'Bernd Dulfer', Str => 'Kapellenstr. 1', EMail => 'bdulfer@sybase.com', Books => [ 'Lord of the Rings', 'Hitchhikers Guide to the Galaxy', 'The Color of Magic' ] }; print 'Other name: ', $address->{Name}, "\n";

Is $address just being used as a reference in this context? What's the difference if I do the following:

my %address = ( Name => 'Bernd Dulfer', Str => 'Kapellenstr. 1', EMail => 'bdulfer@sybase.com', Books => [ 'Lord of the Rings', 'Hitchhikers Guide to the Galaxy', 'The Color of Magic' ] ); print 'Other name: ', %address->{Name}, "\n";

I noticed that the compiler noted that accessing a hash like that is deprecated. The former is a reference, and the following is directly accessing the variable, no? If the latter is deprecated, should I use the former method to always refer to a hash table? Thank you!

Edit: Also, why would adding a key to the hash cause a compilation error? E.g.

my $address{Bernd} = {

causes

syntax error at ./sixth.pl line 14, near "$address{Bernd" Execution of ./sixth.pl aborted due to compilation errors.

What causes that?

Comment on Regarding Initialization and Use of Hashes
Select or Download Code
Re: Regarding Initialization and Use of Hashes
by MidLifeXis (Prior) on Sep 04, 2012 at 17:06 UTC

    %address->{Name} should be $address{Name} in your second example.

    Your first example creates a hash reference, the second creates a hash. $address is about equal to \%address.

    --MidLifeXis

      Ahh, alrighty. That's what I was thinking. I just needed someone to solidify it for me.
      Specifically, the braces around the hash contents in the first assignment return a hash reference. The parentheses in the second assignment give the context of a list, which will be interpreted as key-value pairs when assigned to a hash.
Re: Regarding Initialization and Use of Hashes
by choroba (Abbot) on Sep 04, 2012 at 17:07 UTC
    Yes, $address->{Name} dereferences the hash reference and then searches for the Name key. For a normal hash (%address), you should use
    print $address{Name}, "\n";
    See Perl Data Types and Perl Data Structures Cookbook for details.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Regarding Initialization and Use of Hashes
by Anonymous Monk on Sep 04, 2012 at 18:06 UTC
    In the first example, "$address" is a reference to an otherwise-anonymous hash. In the second, "%address" is the name of a hash.
Re: Regarding Initialization and Use of Hashes
by aitap (Deacon) on Sep 04, 2012 at 18:34 UTC

    my $address{Bernd} = {
    You don't need to explicitly declare hash entries. If you remove my, the hash entry "Bernd" will be assigned to a hash reference without a compilation error.

    If you have not read perlreftut yet, I would recommend to read it. Perl data structures became clear to me after I read this perldoc page.

    Sorry if my advice was wrong.
Re: Regarding Initialization and Use of Hashes
by linuxkid (Sexton) on Sep 04, 2012 at 19:23 UTC

    Always check your sigils!

    --linuxkid


    imrunningoutofideas.co.cc
      Even more important, check your brackets!

      • $variable[1] returns an array element
      • $variable{'one'} returns a hash value
      Both are of course scalars as indicated by the $ sigil.

      CountZero

      A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      My blog: Imperial Deltronics

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://991655]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (18)
As of 2014-10-30 15:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (208 votes), past polls