CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Question about hashes

 



mattmatt
Deleted

Aug 7, 2000, 10:55 AM

Post #1 of 6 (504 views)
Question about hashes Can't Post

alright i have an external text file that is in this outline:

bob
His name is bob
joe
His name is joe

etc.
my question is, how would i, after i store the text file as %stuff, and after i got info. from a form, search to see if the values from the form matched any keys to %stuff, and if they did, how would i print the VALUES of %stuff that the corresponding keys matched the values from the form??

that probably sounds confusing, but say like if the values from the form were "joe" and "bob", i want to see if either of those are keys in %stuff, and if they are, print the values of %stuff that go with the keys that matched

that didnt sound any better did it??

oh well, any help would be appreciated!!
=)
thanks


mattmatt
Deleted

Aug 6, 2000, 10:11 PM

Post #2 of 6 (503 views)
Re: Question about hashes [In reply to] Can't Post

japhy, i just used that right before i posted on the boards, i was almost positive it would work but it didnt... =(

it was tn the exact same thing though, instead of using print "$key => $hash{$key}\n"; i used push(@found, $hash{$key}); then tried printing it out... but i still used what you put said after that didnt work, so can u think of anything i can be doing wrong?

would i have to parse %stuff because i got it from an external file and not from a form?? thats the only thing i can think of that im doing wrong

gus, i havent tried that yet but i probably will in a few seconds after i post this

thanks for all the help,
Matt


gus
Deleted

Aug 7, 2000, 2:43 AM

Post #3 of 6 (504 views)
Re: Question about hashes [In reply to] Can't Post

Hi,

you can use the 'keys' function to find out all the keys in a hash:

@mykeys = keys %stuff;

...then just search through the @mykeys array.

Alternatively, if you know that none of the values in %stuff are empty and you say:

$test = $stuff{'bob'};

...if $test is empty then 'bob' is not a key in %stuff.

Hope I understood what you were asking,

Gus


japhy
Enthusiast

Aug 7, 2000, 6:24 AM

Post #4 of 6 (503 views)
Re: Question about hashes [In reply to] Can't Post

You should never have to go through the list of keys to find a key. Hashes are made for convenient and fast searching:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


if (exists $hash{$key}) {
print "$key => $hash{$key}\n";
}
</pre><HR></BLOCKQUOTE>

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher



wuntvor
Deleted

Aug 10, 2000, 1:25 PM

Post #5 of 6 (503 views)
Re: Question about hashes [In reply to] Can't Post

HAve you chomped all your incoming values so that the keys are "Bob" and not "Bob\n" ?


mattmatt
Deleted

Aug 11, 2000, 8:20 AM

Post #6 of 6 (503 views)
Re: Question about hashes [In reply to] Can't Post

yeah its like the first thing i did =)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives