Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 195
  • Last Modified:

frequency of words

if i have a variable $test that contains a block of text like this

"this is a test file when i will test out this program. the test will
now be set. i have to test out this program as the test will be used when i test out other programs."

and another variable $word which is "test"

(now bear in mind $test can be anything and $word can be anything
so id like a program that works on any example)

what id like to print out is
file 1
out 3
will 2

ie after "test" the word "file" occurs once
the word "out" occurs 3 times and the word "will" occurs twice

would anyone have the code to to print out this data given the
$test and $word variables ?

thanks
0
boofulls
Asked:
boofulls
  • 6
  • 6
1 Solution
 
binkzzCommented:
#! /usr/bin/perl

my $text = "this is a test file when i will test out this program. the test will now be set. i have to test out this program as the test will be used when i test out other programs.";

my $word = "test";




my $templine = $text;

while ($templine =~ m/ $word (\w+)/s)
{
  $templine =~ s/ $word (\w+)//s;
  $item = $1;

  if (!$found{$item})
  {
    $found{$item} = 1;
  } else
  {
    $found{$item}++;
  }
}


foreach $key (keys %found)
{
  print "Word [$key] - Count - [$found{$key}]\n";
}
0
 
boofullsAuthor Commented:
Adjusted points from 100 to 150
0
 
boofullsAuthor Commented:
thanks!
could u adjust the code so
that it can do it for
file test
out test
will test
also (ie the $word is after the result this time
instead of before)

0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
binkzzCommented:
Replace the while loop with:


while ($templine =~ m/ (\w+) $word /s)
{
  $templine =~ s/ (\w+) $word //s;
  $item = $1;

  if (!$found{$item})
  {
    $found{$item} = 1;
  } else
  {
    $found{$item}++;
  }
}
0
 
boofullsAuthor Commented:
Adjusted points from 150 to 200
0
 
boofullsAuthor Commented:
just one final question honestly ;)

say that $word is "hello"
it doesnt seem to pick up on phrases
in $text such as

Hello There

(note the capital H at the start of the "hello"
that is in $text)
can u make sure that it picks up all instances
of the word (in this case "hello") even if it
is "Hello there" or "HELLO there" etc
thanks
0
 
binkzzCommented:
If you want to search in both upper and lower case, add an i after the regular expression:

while ($templine =~ m/ (\w+) $word /si)
{
  $templine =~ s/ (\w+) $word //si;
  $item = $1;

  if (!$found{$item})
  {
    $found{$item} = 1;
  } else
  {
    $found{$item}++;
  }
}
0
 
boofullsAuthor Commented:
thanks
0
 
ozoCommented:
Why if(!$found{$item}) ?
0
 
binkzzCommented:
I wasn't certain if it would work out from 0 as an integer if I would say $found{$item}++ if I had not used $found{$item} before.

Why are you questionning my programming!!! :)

Binkzz
0
 
binkzzCommented:
That was meant to be 'questioning' before you start doubting my spelling as well. 8)
0
 
boofullsAuthor Commented:
what would u recommend ozo?
0
 
binkzzCommented:
$found{$item}++;

Would also work just as well, but I wasn't certain so I added an additional check.

Tom
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now