Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Difference between array and a hash? (Newbie concerns)

by reztlaf (Initiate)
on Jul 30, 2009 at 01:52 UTC ( [id://784449]=perlquestion: print w/replies, xml ) Need Help??

reztlaf has asked for the wisdom of the Perl Monks concerning the following question:

Hello, My background in "coding" stems from coding with the terrible PHP. As such, this has not been clearly exposed to me in the same way as some real concepts should be, as their policy seems to stem from "teaching by removing difficult details". As you can see, I'm attempting to adopt Perl as my main language, and feel I should familiarize myself with the philosophies in the concepts prior to that. So my question to the experienced monks around here is: What is the difference between an array and a hash as 'types', and why would it be a bad idea for them to be merged into the same data structure? In PHP, it is known as an associative array, where it is something like:
$array = array(1, 2, 3, 4); $hash = array('red' => 1, 'blue' => 2', 'green' => 3, 'yellow' => 4); +#hash
But in Perl, it is quite different. If possible, is an in-depth explanation in order? I look forward to an answer to this question.

Replies are listed 'Best First'.
Re: Difference between array and a hash? (Newbie concerns)
by GrandFather (Saint) on Jul 30, 2009 at 02:13 UTC

    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
      Thanks for helping me understand. Regards, Rahul srbworld
      thanks, this was very useful!
Re: Difference between array and a hash? (Newbie concerns)
by merlyn (Sage) on Jul 30, 2009 at 01:59 UTC
    An array is a list of ordered items, useful when the ordering is important (usually).

    A hash is a set of key/value pairs of unordered items, useful when you want to quickly get from a key to its corresponding value no matter how large the data set might get.

    Or, in short: hashes are for mapping, arrays are for ordering.

    -- Randal L. Schwartz, Perl hacker

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this document are to be interpreted as described in RFC 2119.

Re: Difference between array and a hash? (Newbie concerns)
by toolic (Bishop) on Jul 30, 2009 at 02:45 UTC
Re: Difference between array and a hash? (Newbie concerns)
by ig (Vicar) on Jul 30, 2009 at 05:54 UTC
    why would it be a bad idea for them to be merged into the same data structure?

    Sometimes it is a good idea to merge them. For these occasions there are modules that provide the mapping ability of hashes and the ordering ability of arrays in the same data object.

    Often, hash lookup is needed but ordering is not required. In these cases, it would be a waste of resource to maintain un-needed ordering of the hash keys. For small data sets the difference wouldn't be very much but Perl can handle very large hashes. For these large hashes, the work of inserting a new element would be significantly greater if the new key had to be inserted into an ordered list.

    Similarly, if all that is needed is an ordered list of items, the implementation of the numerically indexed array is more efficient than that of the hash lookup. One could use a hash with keys 0, 1, 2, etc. but it would be slower than an array for many operations.

    So, the reason it would be bad to merge arrays and hashes is that doing so would make many common cases less efficient (slower and/or consuming more resource).

Re: Difference between array and a hash? (Newbie concerns)
by hobbs (Monk) on Jul 30, 2009 at 03:02 UTC
    Basically every "array" in PHP is an array and a hash, glued together. All of the values are stored in the hash as key/value pairs, but there's an array stored along with it that gives an ordering to the keys. The thing is, most of the time you either need one behavior (in-order processing) or the other (random lookup), and not both. PHP is giving you the overhead of both at all times; Perl is requiring you to say which one you really want. In the case where you really do need both methods, it's not very hard to set up the necessary parallel data structure in your own code, but Tie::IxHash is another option.
      In perl that used to be called pseudohash, but they were removed.
        Pseudohash goes the other way, though. Pseudohashes were arrays internally, with a hash glued on so that the items could be accessed by key. The "ordered hash" as provided by PHP or IxHash is a hash primarily, with an array glued on to provide an ordering.
Re: Difference between array and a hash? (Newbie concerns)
by ikegami (Patriarch) on Jul 30, 2009 at 03:11 UTC

    Perl hashes are associative arrays. They're called hashes because they're implemented as hash tables.

    If I understood correctly, you called $array = array(1, 2, 3, 4); an associative array. It's not. It's just an array (if that) because the values aren't associated with anything.

Re: Difference between array and a hash? (Newbie concerns)
by roboticus (Chancellor) on Jul 30, 2009 at 02:04 UTC
    reztlaf:

    Actually, in perl it's pretty similar:

    @array = ( 1, 2, 3, 4 ); %hash = ( red=>1, blue=>2, green=>3, yellow=>4 ); print join(" | ", reverse @array); print join(" | ", keys %hash);
    ...roboticus
Re: Difference between array and a hash? (Newbie concerns)
by graff (Chancellor) on Jul 30, 2009 at 04:11 UTC
    I don't really know the relevant internal details for PHP, but I would suppose, based on PHP's "isomorphism" for these two things that Perl treats differently as arrays and hashes, that PHP would automatically protect you from unexpected mayhem in a situation like this:
    # somewhere, some step ends up doing something like: $index = 256 * 2048 * 1024 * 1024 * $i; # for some value of $i >= 1 # and then, shortly afterwards, something like this happens: $array( $index ) = "foobar";
    Presumably, since every "array" in PHP is really just an associative array (whether or not the indexes are numeric), situations like the one above never cause a problem.

    That sort of thing would not cause a problem in Perl either, if you are using a hash. But Perl gives you the opportunity of using an array instead ($array[$index]='foobar' rather than $hash{$index}='foobar'), and when you give a ridiculously large index value for an element in an array, Perl is obliged to make sure that memory is allocated to hold all the array slots between "0" and the given index value.

    So, if you are being careless in your use of (numerically indexed) arrays in perl -- when you should really be using a hash instead -- what is actually happening, in effect, is that perl is giving you enough rope to hang yourself, whereas PHP is allowing you to ignore the fact that your numeric "array" index values may sometimes be insane (and obviously bug-ridden).

    Not a perfect world, either way...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-25 05:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found