Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Use Hash? Array? Still confused after all these years.

by exussum0 (Vicar)
on Jul 21, 2005 at 20:06 UTC ( [id://476982]=note: print w/replies, xml ) Need Help??


in reply to Use Hash? Array? Still confused after all these years.

On a user level, not behind the scenes, a hash and an array are exactly the same thing except one takes arbitrary strings/scalars as their keys and arrays uses only non negative numbers. There are nuances to how they are used in perl, but in a general sense this is true.

If you can easily map everything to the non-negative numbers integers from 0 through some length/count of the items you have, an array is great. Examples are an already sorted list of names. Want to know who's 5th, just do $names[5]. Arrays hold order very well, since they map to non-negative numbers and are kinda designed as a continuous data structure.

If you need arbitrary access to information where order or mapping isn't easy or makes sense, a hash is great. For instance, if I put my name, address and other personal info in a single structure, a hash rocks. $person{first_name} would give me a person's first_name.

To give the counter examples of when they suck, keeping a sorted list of people in a hash would be a little odd. I can do $person{5}, but what would $person{beer} mean in the context? Should it be allowed? It's just, strange.

Using an array for data that has no easy mapping to say, a function that is natural, an array kinda sucks. For instance, $person[0] being the first name, $person[1] being the last isn't as natural as $person{first_name}.

The rest is just engineering. They are both fairly fast, it's a matter of seeing which tool fits the job better.

Update: Wtf.. I used number when I meant integer. I'm an idiot. What i meant and typed were inconsistent. Thanks brian_d_foy.

----
Give me strength for today.. I will not talk it away..
Just for a moment.. It will burn through the clouds.. and shine down on me.

Replies are listed 'Best First'.
Re^2: Use Hash? Array? Still confused after all these years.
by brian_d_foy (Abbot) on Jul 21, 2005 at 21:45 UTC

    Well, hashes aren't exactly the same thing. Arrays are ordered, and hashes aren't. Many people still wonder why keys() doesn't return the keys in the order they added them (or even the same order all the time), and it will always be something I have to emphasize in a beginning Perl class.

    I don't like to say that arrays are indexed by non-negative integers either (and I've always wondered why people use "integer" just to ignore half of them). We can use the negative indices to count from the end as long (as we have enough entries to count).

    --
    brian d foy <brian@stonehenge.com>
      You are right on all counts. In c, you create a buffer overflow (underflow?) unless you are careful w/ your pointer arithmatic. php I believe doesn't mind. Perl, yeah.

      It's fairly universal to say non-negative numbers though, on the concept level. But that's a preference in teaching the concepts, eh? :)

      ----
      Give me strength for today.. I will not talk it away..
      Just for a moment.. It will burn through the clouds.. and shine down on me.

        I don't say "non negative numbers" either, because you can't use most "non negative numbers" for array indices. I say "counting numbers", "whole numbers", or "cardinal numbers" depending on my mood. It's not really a teaching preference so much as accurately describing the concept.

        --
        brian d foy <brian@stonehenge.com>
      Wes still index arrays using non-negative integers. Sure, you can use negative integers to count from the end, but $array[-$N] is just syntatical sugar for $array[@array-$N]. If we truely could use negative integers to index, the following would not be an error:
      my @array = (1); $array[-2] = 2;

Log In?
Username:
Password:

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

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

    No recent polls found