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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Understanding Hashes

Status
Not open for further replies.

PerlNewUser

Technical User
Apr 17, 2008
9
0
0
US
After reading this very helpful forum for a few weeks, and now I finally have a question!

I am learning Perl and don’t fully understand hashes. I understand that they can be used in place of arrays to simultaneously compare and change data, but which part of the hash expression does what? For example:

Code:
%Hash(Value1, value2, value3)
$MyData = (exists $hash{$MyData}) ? $Hash{$MyData} : ‘Or Else Print This’;

I don’t follow how values are assigned. Also, can more than one data element be compared if I want the value of $MyData to depend on the existence of Two or More elements within the hash, i.e.

$DesiredValue=(exists $hash{$Mydata1} && exists $hash{MyData2})
 
Your sample code is just wrong. There is no hash in the code you posted. Maybe it was meant to be psuedo code but even then it would be a poor example.

Simple Example, using a name:

John Smith

Assume that is you. You know a lot of things about you because of your name: age, sex, date of birth, race, religion, etc etc etc

In hash form:

Code:
%John_Smith = (
   age  => 28,
   sex  => 'male',
   race => 'caucasion', 
)

Note that a hash must have an equal number of key and value pairs (keys on the left of '=>' and values on the right).

To know you age:

Code:
print $John_Smith{age};

To change your age:

Code:
$John_Smith{age} = 29;

To add another key/value pair to a hash is the same as changing one that already existed when the hash was first created:

Code:
$John_Smith{married} = 'yes';

Thats to give you the general idea of what a hash is, it is a list of keys that have a value associated with them.









------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
I based my example from this thread, where it was described as a hash:

Below is how it was used. I understand you explanation well enough, but still need to read deeper into these threads to understand it better.

Code:
“I'd suggest using a hash to store your state abbreviations”

[CODE]
open(MAP,"<$file")||die "Could not open $file";
    while (<MAP>) {
        chomp;
        ($code, $state, $desc)=spilt /\t/, $_;
        $states{$state}=$desc;
    }
close MAP;
 
And, in digging deeper, I found another one. In this one, I see there is a %periodic, which I did not see in the previous example I posted. Using the definition you gave me, I see that the %periodic defines the hash, and the $element and $weight are the values, correct?

Code:
Use a hash to provide a lookup for the atomic weights

my %periodic;

while (<DATA>) {
   chomp;
   my ($element, $weight) = split;
   $periodic{$element} = $weight;
}

source:
 
In that element, weight example, element is the key, and weight is the value.



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I gave you a pretty good example of what a hash is. It is simply a list of key/value pairs. The hash name is used to know which hash table the script is getting data from or putting data into, in my example that would be %John_Smith. Trying to figure out what a hash is by looking at the code that was posted is going to be difficult because those posts are not tutorials that explain the basic concept of what a hash is. They assume the user already knows what a hash is.

Here is an online book you might find useful:


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top