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

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Match hash keys and a scalar



May 13, 2013, 9:59 PM

Post #1 of 3 (450 views)
Match hash keys and a scalar Can't Post

Hi guys :) I've charged a hash with a text file, with one string per line, like this:

open(IN,"<text.txt") or die "Dommage!\n"; 
while (my $desc = <IN>){
chomp $desc;
$hash{$desc} =1;

the file text.txt is like this:
i have
you do
she has
we will go to
but he is nice
I would like to match these keys with a text wich is in a scalar, like this:

$var = "Today, we will go to Paris. I have met Peter there. He is odd but he is nice" 
Bold is for the parts wich match with the hash keys. I would like to count how many times a key from my hash appears in the scalar string (using a $n++). Is there a way to do this ? Help plz!!!

Veteran / Moderator

May 14, 2013, 3:49 AM

Post #2 of 3 (433 views)
Re: [IsabelleFr] Match hash keys and a scalar [In reply to] Can't Post

Hi Isabelle,

I assume from your login ID that you probably speak French.

Look at this link:

It is a long post on a French Perl forum where I and another chap have proposed various solutions to a very similar problem.


May 14, 2013, 6:33 AM

Post #3 of 3 (427 views)
Re: [Laurent_R] Match hash keys and a scalar [In reply to] Can't Post

I assume that you want the counts in the hash.

$hash{$_} = my @temp = $var =~ /$_/i foreach (keys %hash) ;

Good Luck, 


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

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