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

Arrays and hashes. 1

Status
Not open for further replies.

EchoCola

Programmer
Apr 13, 2004
48
US
Hello,

I'd like to do something but im not quite sure how. I think i might need to use a hash table.

Suppose I have a couple arrays

Code:
@array1 = ("apple","orange","grape");
@array2 = ("dogs","cats","birds");

I would like to be able to search for example "apple" and get a key value of "fruit". Or search for dog and return a value of "animal".




 
I'm guessing I would build the hash table such that:

fruit is the key and the value would be array1
animal the key and value would be array2


 
Load those arrays into a single hash...
Code:
my %lookup;
$lookup{$_} = "fruit" for ("apple","orange","grape");
$lookup{$_} = "animal" for ("dogs","cats","birds");
print $lookup{"dogs"};
 
brigmar,

the "for" loops are not doing anything except to make the code a bit confusing.

EchoCola,

Your question is a bit vague. Hash keys can be pointers to arrays, but then the hash key "fruit" will point to an array reference, not the name of a fruit. You would have to search through the array the hash key points to, to see if "apple" or other fruit is in the list of fruits. Some code to ponder:

Code:
my %hash = (
   fruits => [qw(apple orange grape)],
   animals => [qw(dog cat bird)],
);

if ( grep {$_ eq 'apple'} @{$hash{'fruit'}} ) {
    print "There is an apple in the list of fruits\n";
}
else {
    print "No apples here\n";
}

If you are not already versed in references the code will look strange. Its all just an example, its not meant to be the best example of how to do something like this.

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
A small error in the above code, should be:

Code:
my %hash = (
   fruits => [qw(apple orange grape)],
   animals => [qw(dog cat bird)],
);

if ( grep {$_ eq 'apple'} @{$hash{'fruit[red]s[/red]'}} ) {
    print "There is an apple in the list of fruits\n";
}
else {
    print "No apples here\n";
}

------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
horses for courses...
I thought the code was pretty simple and reads like English.


 
Sorry Kevin, I'm with brigmar on this one. His1 code is simple, clean, and meets the requirements [smile]

Obviously if this was anything more than an example, we'd want to load the data from a file of name/value pairs to make it more flexible...

1 assumption: (I'm being very PC today as it looks like I might have been red flagged on another post yeterday...)

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Sorry brigmar,

Your code is fine. I was probabaly posting too quickly and didn't take the time to read the original question correctly. My code won't even do what the OP asked.

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

Part and Inventory Search

Sponsor

Back
Top