Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

clarification about hashes. 2

Status
Not open for further replies.

LetMeHandle

Technical User
Sep 23, 2001
1
IL
(as start i'm sorry for my poor english...);
i am a programmer with C, and i know about arrays, strings, etc.
now i started learning perl and i cant find a good answer about what is that so good aboud hashes?
i heard that pulling out values from a hash is fast- o.k... but :
1. can i use an array as a value(like database) ?
2. if i need to hold/store information that has one key and several values(like matrix) does the use of hashes is the right choise?, or should i use array of 3th,4th degree?
3.i need some order in that subject to understand the principel of this uniqe type....
 
Hi,

I'm still quite new at perl, but I must say that hashes are GREAT to work with.

A hash can basically contain anything...

Standard hash:[tt]
%hash = ("1","Jan","2","Feb","3","Mar");[/tt]

This creates a 2 level hash:[tt]
$hash{key}{name} = "thierry";
$hash{key}{initials} = "tve";[/tt]

This is a hash that contains an array:[tt]
@{$hash{path}} = qw( one two three four);[/tt]

I could keep giving examples forever...

DataDumper is a good way to see the contents of variables:[tt]
use Data::Dumper;
print Dumper(\%hash);:[/tt]

I like hashes because they are clear, self-speaking ($password{tve} = "1234" is obvious), very easy to handle uniqeness,...

Thierry



 
To get our terminology consistent.
An array is used to mean a simple list of values that can be identified by their position in the list.
@array = ('item1','item2','item3');
$second_item = $array[1]; # counting from 0

A hash is also called an 'associative array' which is composed of a series of keys and associated values.
%hash = ('key1'=>'value1','key2'=>'value2');
$value2 = $hash{$key2};

You can mix arrays and hashes to get arrays of arrays, or arrays of hashes, or hashes of arrays, or hashes or hashes.

One of your questions,
2. if i need to hold/store information that has one key and several
values(like matrix) does the use of hashes is the right choise?, or should
i use array of 3th,4th degree?


This is an example of a hash of arrays, or one key and several values.
#!/usr/local/bin/perl
@array1 = ('a','b','c','d');
@array2 = ('e','f','g','h');
@array3 = ('i','j','k','l','m');

# an associative array whose values are references to
# the above arrays
%mem_structure = (
'first' => \@array1,
'second' => \@array2,
'third' => \@array3);

# pulling values back from the memory structure
foreach $key (keys(%mem_structure))
{
print "$key
0th - $mem_structure{$key}[0]
1st - $mem_structure{$key}[1]
2nd - $mem_structure{$key}[2]
3rd - $mem_structure{$key}[3]\n\n";
}
print "The last element in the third array is '$mem_structure{'third'}[4]'\n";

HTH Please use descriptive titles and check the FAQs.
And, beware the evil typo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top