Solved

frequency of words

Posted on 2000-03-20
13
186 Views
Last Modified: 2010-03-05
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
Comment
Question by:boofulls
  • 6
  • 6
13 Comments
 
LVL 4

Accepted Solution

by:
binkzz earned 200 total points
ID: 2636208
#! /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
 

Author Comment

by:boofulls
ID: 2636750
Adjusted points from 100 to 150
0
 

Author Comment

by:boofulls
ID: 2636751
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 4

Expert Comment

by:binkzz
ID: 2636796
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
 

Author Comment

by:boofulls
ID: 2639488
Adjusted points from 150 to 200
0
 

Author Comment

by:boofulls
ID: 2639489
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
 
LVL 4

Expert Comment

by:binkzz
ID: 2639498
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
 

Author Comment

by:boofulls
ID: 2639707
thanks
0
 
LVL 84

Expert Comment

by:ozo
ID: 2640175
Why if(!$found{$item}) ?
0
 
LVL 4

Expert Comment

by:binkzz
ID: 2640203
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
 
LVL 4

Expert Comment

by:binkzz
ID: 2640214
That was meant to be 'questioning' before you start doubting my spelling as well. 8)
0
 

Author Comment

by:boofulls
ID: 2640515
what would u recommend ozo?
0
 
LVL 4

Expert Comment

by:binkzz
ID: 2640940
$found{$item}++;

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

Tom
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SIMPLE Perl Regex 1 157
delete query using perl dbi 3 100
Export Variables in Perl 3 67
parse a file and get data out 11 39
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now