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:
Match hash keys and a scalar

 



IsabelleFr
Novice

May 13, 2013, 9:59 PM

Post #1 of 3 (282 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:

Code
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
etc
I would like to match these keys with a text wich is in a scalar, like this:

Code
$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!!!


Laurent_R
Veteran / Moderator

May 14, 2013, 3:49 AM

Post #2 of 3 (265 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: http://www.developpez.net/forums/d1265727/langage/chercher-chaine-caracteres-remplacement/

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


BillKSmith
Veteran

May 14, 2013, 6:33 AM

Post #3 of 3 (259 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) ;

Code
Good Luck, 
Bill

 
 


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

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