Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > differences between hashes and arrays ?

Reply
Thread Tools

differences between hashes and arrays ?

 
 
Jack
Guest
Posts: n/a
 
      09-22-2006
Hi folks I am not an expert in perl but correct me if I am wrong -

is it true you can use a hash and work with it just as you would an
array - what are the differences between them (besides in an array you
can have a multidimensional array) ?

why arent folks using hashes instead of arrays since (I believe) they
are faster to access and take up the same or less memory than arrays..

Thanks,

Jack

 
Reply With Quote
 
 
 
 
Mirco Wahab
Guest
Posts: n/a
 
      09-22-2006
Thus spoke Jack (on 2006-09-22 16:55):

> is it true you can use a hash and work with it just as you would an
> array - what are the differences between them (besides in an array you
> can have a multidimensional array) ?


There is a similar interface, but thats almost all.

==> http://en.wikipedia.org/wiki/Hash_table

In short, a 'hash table' is a complicated organism
which allows you to find an item (say: key, stored
in there) in very very short time, which is the point.

An "array" means otherwise "access by an index number",
so array elements must be searched in order to
find a particular item.

compare:

my $item = $hash{ 'ITEM_OF_INTEREST' }; # gets the item

vs.

SEARCH: foreach $element (@array) {
if($element eq 'ITEM_OF_INTEREST') {
$item = $element; # gets the item
last SEARCH;
}
}

> why arent folks using hashes instead of arrays since (I believe) they
> are faster to access and take up the same or less memory than arrays..


No, folks using hashes if hash tables are appropriate
and arrays if arrays are ...

Say, you have ten million 3D coordinate points
which are in some order already (an order that
means something to you). Why would you bother to
put them in a hash table, if all you need is sub-
sequent access to the 13244'st or 10'th to 1923331'th?

Regards

Mirco
 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      09-22-2006
Jack <(E-Mail Removed)> wrote:

> is it true you can use a hash and work with it just as you would an
> array -



No.


> what are the differences between them



Arrays are ordered and indexed with numbers.

Hashes are UNordered and indexed with strings.


> (besides in an array you
> can have a multidimensional array) ?



You can also have a multidimensional hash, so that is not
one of the differences.


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      09-22-2006
Jack wrote:
> Hi folks I am not an expert in perl but correct me if I am wrong -


You're wrong.

> is it true you can use a hash and work with it just as you would an
> array


No.

> - what are the differences between them


A hash is by definition unordered. There is no ordering of any kind to
the key/value pairs in a hash. An array is ordered - there is a very
well defined sequence of which item follows which item. Therefore, you
can treat an array like a stack or a queue (using the push/pop
shift/unshift set of functions), while this is not possible with a
hash. With a hash you can associate to distinct pieces of data in one
structure, and can retrieve a specific value in very efficient time.

An array is indexed by positive integers. A hash is keyed by any
string, whether its contents are numeric or not.

> (besides in an array you
> can have a multidimensional array) ?


Ironically, that is *not* one of the differences. You can have
multidimensional hashes as well. In fact, you can have hashes of
arrays, arrays of hashes, arrays of hashes of hashes, hashes of arrays
of hashes, or any other combination you'd like.

> why arent folks using hashes instead of arrays since (I believe) they
> are faster to access and take up the same or less memory than arrays..


Faster to access, yes. Same or less memory, no.

These are not apples to apples. You use a hash and an array for
completely different purposes.

If you have an example of a program or algorithm you're working on, we
could help you get rid of some of your misperceptions. . . .

Paul Lalli

 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      09-22-2006
"Jack" <(E-Mail Removed)> wrote:
> Hi folks I am not an expert in perl but correct me if I am wrong -
>
> is it true you can use a hash and work with it just as you would an
> array


That would depend on what you are doing. For some things, you could easily
use them interchangably. For most things, you can't. If youre indices are
not integers, you can't use arrays. When your indices are integers but are
not densely packed but need to accessed as though they were, you cannot
readily use hashes.


> - what are the differences between them (besides in an array you
> can have a multidimensional array) ?


That is not a difference between them (in Perl).

> why arent folks using hashes instead of arrays since (I believe) they
> are faster to access and take up the same or less memory than arrays..


Wrong on both counts. When arrays and hashes are interchangable, hashes
are much slower and take substantially more memory.


Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
Jack
Guest
Posts: n/a
 
      09-22-2006

Michele Dondi wrote:
> On 22 Sep 2006 07:55:56 -0700, "Jack" <(E-Mail Removed)> wrote:
>
> >Hi folks I am not an expert in perl but correct me if I am wrong -

>
> Seeing your question and your considerations below, indeed you seem
> not to be an expert in perl. So I guess you're not wrong.
>
> >is it true you can use a hash and work with it just as you would an
> >array - what are the differences between them (besides in an array you
> >can have a multidimensional array) ?

>
> You can also have multidimensional hashes. But both are not "real"
> multidimentional beasts. However for the moment don't bother...
>
> Really hashes are associative arrays, i.e. they are a finite mapping
> between a set of _keys_ and a set _values_. The keys are generic
> strings. An array is a mapping between a finite set of natural numbers
> of the form {0,... , n} (if you don't set $[ to something other than
> 0, but again, don't bother!) and a set of values.
>
> >why arent folks using hashes instead of arrays since (I believe) they
> >are faster to access and take up the same or less memory than arrays..

>
> You will certainly know that in perl conversions between numbers and
> strings happen automagically all the time. So indeed you may use a
> hash to implement an array. However hashes are called like that
> because their implementation is based on hash tables that provide
> means to quickly search through the keys. There's no particular
> general advantage when the keys are simple numbers instead.
>
> So use an array when you need a mapping from a finite set of numbers
> to some values, except possibly when the mapping is very sparse. How
> much is "very", may depend on many factors, and I don't know the lower
> limit exactly. Surely I would use a hash if I need to *explicitly* set
> the mapping amongst say 100 numbers uniformely distribuited in
> 0..100_000_000 and some values. I don't have the vaguest idea how well
> the hashing algorithm would hash such keys, but the thing can be
> investigated...
>
>
> Michele
> --
> {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
> (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
> .'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
> 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,


Thanks for the comments all. For these applications, which is better
suited, array or hash, and assume a very large volume of data and
consider memory :
1- iterate through a huge list (hash or array?) of values and compare
each value in the list, 1 by 1, to a match condition test like
m/^\d.*?\d$/ (all digits), and then keep the count of the values that
pass the match test
2- compare all values in column 1, file 1 (one value at a time ; assume
10M records ) to determine if the value exists in the target list (hash
or array?) from column 2 file 2 (assume column 2 is 20M records)

Thank you,
Jack

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      09-22-2006
Jack wrote:
> Thanks for the comments all. For these applications, which is better
> suited, array or hash, and assume a very large volume of data and
> consider memory :
> 1- iterate through a huge list (hash or array?) of values and compare
> each value in the list, 1 by 1, to a match condition test like
> m/^\d.*?\d$/ (all digits), and then keep the count of the values that
> pass the match test


array. You just want to iterate over all of them, in sequence. No
reason for the overhead of a hash.

my $count = grep /^\d.*?\d$/ @great_big_array;

(by the way, you do know that your pattern does not match "all digits",
right? It matches anything that starts with and ends with a digit, of
at least length two. The "inside" of the value can be literally
anything.)

> 2- compare all values in column 1, file 1 (one value at a time ; assume
> 10M records ) to determine if the value exists in the target list (hash
> or array?) from column 2 file 2 (assume column 2 is 20M records)


column1, file1 - array. You want to iterate over all of them, one at a
time, in sequence.

column2, file2 - hash. You want to access a specific element of your
set, without knowing or caring where in the set that element falls.

for my $value (@column1){
if (exists $column2{$value}) {
print "$value exists in column2 file2\n";
} else {
print "$vlaue does not exist in column2 file2\n";
}
}

Paul Lalli

 
Reply With Quote
 
Brian McCauley
Guest
Posts: n/a
 
      09-22-2006

(E-Mail Removed) wrote:
> "Jack" <(E-Mail Removed)> wrote:
> > why arent folks using hashes instead of arrays since (I believe) they
> > are faster to access and take up the same or less memory than arrays..

>
> Wrong on both counts. When arrays and hashes are interchangable, hashes
> are much slower and take substantially more memory.


Except in the rare and special case of a very sparsely populated array.

 
Reply With Quote
 
Henry Law
Guest
Posts: n/a
 
      09-22-2006
Paul Lalli wrote:
> Jack wrote:
>> Hi folks I am not an expert in perl but correct me if I am wrong -

>
> You're wrong.
>
>> is it true you can use a hash and work with it just as you would an
>> array

>
> No.


Of course all the material about the difference in the ways that hashes
and arrays is perfectly correct, but there is one sense in which the OP
is correct - or at least has some method in his madness. Look:

# -------------------------------------------
#! /usr/bin/perl

use strict; use warnings;

use Data:umper;

my @array = ("one",111,"two",22);
# Or we could even have coded my %hash = ("one",111,"two",22);
my %hash = @array;

print Dumper(\%hash);
# --------------------------------------------
F:\>tryit.pl
$VAR1 = {
'one' => 111,
'two' => 22
};


--

Henry Law <>< Manchester, England
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      09-22-2006
Henry Law wrote:
> Paul Lalli wrote:
> > Jack wrote:
> >> Hi folks I am not an expert in perl but correct me if I am wrong -

> >
> > You're wrong.
> >
> >> is it true you can use a hash and work with it just as you would an
> >> array

> >
> > No.

>
> Of course all the material about the difference in the ways that hashes
> and arrays is perfectly correct, but there is one sense in which the OP
> is correct - or at least has some method in his madness. Look:
>
> # -------------------------------------------
> #! /usr/bin/perl
>
> use strict; use warnings;
>
> use Data:umper;
>
> my @array = ("one",111,"two",22);
> # Or we could even have coded my %hash = ("one",111,"two",22);
> my %hash = @array;


None of this shows "using a hash and array in the same way". This is
populating an array with a list of values, and then populating a hash
with the list of values that are currently contained in an array.
%hash and @array have nothing to do with each other before or after
this statement.

Make sure you read and understand
perldoc -q difference
"What is the difference between a list and an array"

Paul Lalli

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Showing the differences between 2 hashes Rick Tan Ruby 1 12-23-2010 03:36 AM
"Pseudo-hashes are deprecated" error and accessing a hash of hashes ernestm@mindspring.com Perl Misc 3 01-31-2006 04:40 AM
Hash of hashes, of hashes, of arrays of hashes Tim O'Donovan Perl Misc 5 10-28-2005 05:59 AM
Differences between one-dimensional arrays in Java and C Paul Morrison C Programming 15 05-06-2005 05:35 PM
Differences between one-dimensional arrays in Java and C Paul Morrison Java 1 05-01-2005 04:37 PM



Advertisments