Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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

In reply to Re: Difference between array and a hash? (Newbie concerns) by GrandFather
in thread Difference between array and a hash? (Newbie concerns) by reztlaf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-19 06:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found