Fun with Perl #3 – Hashes

Posted on Updated on

Managed to finish my work a bit early so I get to play around with Perl again … for a little bit. I’m trying to get my head around object orientation in this language but it looks … strange still … so I need more time to get used to that first.

Today I looked at Hashes because I never really use this tool in any language. The closest I have ever used is probably a dictionary in Python and/or a map in C++. Basically, a Hash in Perl is a dictionary in Python with a key and value definition but the syntax is a little different. The idea is very easy to understand.

In Perl you make a hash like this:

%hash = ("UK", 1, "HK", 2, "CND", 3);

and in Python a dictionary, which works the same way, is created like this:

names = {“UK”: 1, “HK”: 2, “CND”: 3}

the strings UK, HK and CND are keys, which have int values each. Printing out the Hash/dictionary contents will look like below:

UK - 1
HK - 2
CND - 3
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s