Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Difference between array and a hash? (Newbie concerns)

by GrandFather (Saint)
on Jul 30, 2009 at 02:13 UTC ( [id://784452]=note: print w/replies, xml ) Need Help??


in reply to Difference between array and a hash? (Newbie concerns)

Arrays and hashes are both bunches of pigeon holes where you can store values. The difference is how you get at any particular pigeon hole.

For an array each pigeon hole has a number (the 'index') which ranges from 0 to one less than the number of pigeon holes.

For a hash each pigeon hole has a label and you access the pigeon hole by whatever is written on the label. I understand that PHP only has labeled pigeon holes (hashes), but of course if you label them with a sequence of numbers from 0 to n - 1 the effect is somewhat like using an array (but less efficient in various ways).

Perl arrays are fairly efficient for implementing things like stacks and queues using push, pop, shift and unshift. They are also easy to access from either end - $array[-1] is the last element of a (non-empty) array for example. They are good for storing things where the order is important. Arrays are a poor choice if you need to search for stuff in them often.

Perl hashes are good for storing things you want to get at by name and where order is unimportant. They are an excellent choice where you need to determine if you already have an entry of a particular name already which often makes them the tool of choice when you need to look stuff up frequently.


True laziness is hard work

Replies are listed 'Best First'.
Re^2: Difference between array and a hash? (Newbie concerns)
by Anonymous Monk on Mar 30, 2010 at 06:48 UTC
    Thanks for helping me understand. Regards, Rahul srbworld
Re^2: Difference between array and a hash? (Newbie concerns)
by Sosi (Sexton) on Mar 10, 2014 at 10:11 UTC
    thanks, this was very useful!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://784452]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (2)
As of 2024-04-24 17:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found