On 10 February 2016 at 03:46, James Kerwin <jkerwin2...@gmail.com> wrote:
> (I'm a bit wary of hashes because they're weird).


If you want a nice way to reason about hashes when you're really new,
there's something that you probably already understand you can borrow
understanding from:

Folders.

A hash is like a folder.

And every key is like a file.

And you can trivially demonstrate this with this code that treats a
folder as a storage mechanism.

For every item you see:

1. Look in the folder for a file with the same name.
2. Does it exist? No, then its zero
    yes? Then read what number it currently has in it
3. Add one to that number
4. write that file out again.
5. Append that number to the item.

https://gist.github.com/kentfredric/8b90c88e20c7af9b1252


Now, this has the obvious limitation that this data structure persists
its data between runs, so every time you run the script, the numbers
will get bigger, and you'll have to nuke that script between runs.

But that's basically what the hash is doing for you.

Its a collection ( Folder / Hash ), ordered by keys ( like filenames
), that contain data ( like file contents ).

The only distinction you need to immediately worry about is that
Hashes happen entirely in memory, and they evaporate
as soon as your program stops.

I wouldn't recommend using the file technique for any real purpose,
but its interesting from an education perspective.


-- 
Kent

KENTNL - https://metacpan.org/author/KENTNL

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to