Homework: hashes

Hashes (hw4)

Remember hashes? They are like arrays, a collection of boxes. The main difference is that each box has a name instead of a number. Today’s homework is about hashes!

A bit of theory

So if this is an array:

|_| 0
|_| 1
|_| 2
|_| 3
|_| 4
|_| 5
|_| 6
|_| 7
|_| 8
|_| 9

a hash would be something like this:

|_| 'pippo'
|_| 'lampadina'
|_| 'anche numeri vanno bene'
|_| 1443
|_| 'pasta col pesto'
|_| 'altri numeri'
|_| 211
|_| 889
|_| 'che ne so'

The name of the box is called key and can be either a string or a number.
Scalar (normal) variables start with ‘$’, arrays start with ‘@’ and hashes have ‘%’. When you want to get an element from the hash you do it like this:

$nome_hash{'pasta col pesto'}

where ‘pasta col pesto’ is the key.

On to the homework

The code for this homework is hw4, but we expect the creative ones among you to surprise us it with innovative codes, like hw04! Seriously, lets see if this time everyone can get it right.

Lets start from this program:

for ($i = 0; $i < 1000; $i++) {
  $j = int(rand()**3*1000);
  print "$j\n";
}

It prints one thousand random numbers from 0 to 999. Try it a couple of times.

Exercise 1 (perl1): even zero counts!

You may have noticed that zero appears pretty often in the numbers. Maybe something is wrong and the numbers are not so random as we thought.

In order to check this, change the program like this: add a variable called $numero_di_zeri and use it to count the number of zeroes and print it at the end of the program. Do the same also for the number of ones, eights, ninety-nines and five hundreds. Count each number separately!

Are they different from what you expected? Lets investigate better!

Exercise 2 (perl2): counting with hashes

It would be nice to count all numbers, but you would need a lot of variables to do that. A better way is to use a hash!

Change the perl1 program so that, instead of using a scalar variable for each number, it uses one hash called %numero_di_numeri. The idea is that each box in the hash contains the occurrences and the name or key of the box is the number that occurs that many times. For instance, instead of using the scalar variable $numero_di_zeri, we use a box in the hash:

$numero_di_numeri{0}

At the end print each counter with the following loop:

foreach $j (sort keys %numero_di_numeri) {
  print "$j: $numero_di_numeri{$j}\n";
}

Note that we used the function keys to get the keys of the hash, so we can loop on the keys with foreach. Note that hashes are not ordered like arrays, so it is normal

The program should print the number of occurrences of each number that occurred at least once (you may want to read this sentence again). Now that you have all the values, can you guess why they are like these?

How to submit your work

Once you checked that your script is okay, you must submit the work done filling this form. Remember to put your login ID, and the correct homework ID (this is hw4, okay?).

Tagged

4 thoughts on “Homework: hashes

  1. marco necci says:

    nel mio hw4/script2 c’è un errore nella porzione di script che andava copiata dal blog, manca il “keys”…è perché ho per sbaglio mandato una versione di prova in cui l’avevo rimosso per vedere cosa succedeva al comando sort; il resto funziona comunque 🙂 ciao

  2. Gloria says:

    Ciao! Domanda: ma il secondo punto bisogna farlo per tutti i numeri generati da 1 a 1000 oppure sempre solo per 0, 2, 8 ecc?

    • proch says:

      Ciao, non capisco da dove si genera il dubbio… Il secondo punto chiede in pratica di dire “quante volte” compare ciascun numero estratto.

Leave a Reply

Your email address will not be published. Required fields are marked *