RE's and Hashes
Hi
I'm learning perl and have hit problems fairly early on. I have typed a program into perls editor that is suppost to determine the frequency of the occurence of each word in a string.
Once i have saved the program and run it i get a warning about a variable being used only once. If i proceed and type in the string "One Two Two Three" (without the quotes), nothing happens and the programme wont exit. the only way to get out of it is to close down the MS DOS window. i have written the code below, i would be greatfull if somebody could tell me the problem with it.
It would be realy helpful to know why this doesnt work, and not to recieve any advice on programs that would do the same thing.
THANX
#!C:\perl\bin\perl.exe
while ($line = <STDIN> )
{
while ($line =~ s/(\w+)(.*)/$2/)
{
$word = $1;
$wordHash{$word}++;
}
}
while ( ($word, $count) = each(%wordHash) )
{
$wordArray[$i] = "$word\t$count";
$i++;
print ("$word\n");
}
whitehawk posted this at 19:22 — 27th August 2006.
She has: 2 posts
Joined: Aug 2006
In your regular expression, /(\w+)(.*)/$2/), the (.*) bit will match everything, therefore never end (note that regular expressions are greedy, and will match the longest possible string).
What you probably want is something more like
([a-zA-Z]*) or (\w*) (match any alphabetic characters)
OR (([^\s]*) i.e. match anything that's not whitespace.
OR you could use the word boundary thingie, \b ... eg ... (\b.*\b).
I haven't tried either of these out, but you get the general idea.
Cheers,
Julie
whitehawk posted this at 19:23 — 27th August 2006.
She has: 2 posts
Joined: Aug 2006
D'oh! Just noticed the date on the post I just replied to ... think I might be a bit late to help!!
j.
Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.