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:
Hash to match a for loop!

 



IsabelleFr
Novice

May 13, 2013, 10:02 AM

Post #1 of 4 (304 views)
Hash to match a for loop! Can't Post

Hi, boyz! I have a problem and hope that some1 can help me. Well, i'm trying to use a hash (wich i charged previously) to match table items. It's something like this:

Code
open(IN,"<list.txt") or die "Dommage!\n"; 
while (my $desc = <IN>){
$etat{$desc} =1;
}
close IN;

then i charge a table with some info and read it using a for loop like this:

Code
for (my $i = 0; $i <=$#tab; $i++){ 
if (defined ($etat{$tab[$i]})){
$abc++;
$total_abc++;
}

I just can't make it match the hash keys with the table items. it's like, if i have in my hash the key "nice" i would like to, when i make the for loop and if it encounters the word "nice", it increments my two variables ($abc and $total_abc). Thanks for helping :)


Laurent_R
Veteran / Moderator

May 13, 2013, 10:41 AM

Post #2 of 4 (301 views)
Re: [IsabelleFr] Hash to match a for loop! [In reply to] Can't Post

Hi Isabelle,

try to chomp your lines before loading them in either the hash or the table. The problem is likely to come from new line characters.


(This post was edited by Laurent_R on May 13, 2013, 11:00 AM)


BillKSmith
Veteran

May 13, 2013, 6:40 PM

Post #3 of 4 (271 views)
Re: [Laurent_R] Hash to match a for loop! [In reply to] Can't Post

You can simplify the counting by using grep in scalar context. (You still need Laurent's newline fix)


Code
chomp @tab; 
$total_abc += grep {exists $estat{$_}} @tab;

Good Luck,
Bill


IsabelleFr
Novice

May 13, 2013, 9:46 PM

Post #4 of 4 (264 views)
Re: [Laurent_R] Hash to match a for loop! [In reply to] Can't Post

Hi! Thanks a lot! It worked :))

 
 


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

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