0

How can I find the number of times a word is in a block of text in Perl?

For example my text file is this:

#! /usr/bin/perl -w
# The 'terrible' program - a poorly formatted 'oddeven'.
use constant HOWMANY => 4; $count = 0;
while ( $count < HOWMANY ) {
  $count++;
  if ( $count == 1 ) {
    print "odd\n"; 
  } elsif ( $count == 2 ) { 
    print "even\n";
  } elsif ( $count == 3 ) {
    print "odd\n";
  } else { # at this point $count is four.
    print "even\n";
  }
}  

I want to find the number of "count" word for that text file. File is named terrible.pl

Idealy it should use regex and with minimum number of line of code.

EDIT: This is what I have tried:

use IO::File;
my $fh = IO::File->new('terrible.pl', 'r') or die "$!\n";
my %words;
while (<$fh>) {
  for my $word ($text =~ /count/g) {
  print "x";
    $words{$word}++;
  }
}
print $words{$word};
kamaci
  • 72,915
  • 69
  • 228
  • 366
  • 2
    Please take your best stab at it and, if you still have questions or problems, come back here. This is not a "we'll do your homework for you" site. – David Thornley Mar 15 '11 at 20:16

6 Answers6

6

Here's a complete solution. If this is homework, you learn more by explaining this to your teacher than by rolling your own:

perl -0777ne "print+(@@=/count/g)+0" terrible.pl
LHMathies
  • 2,384
  • 16
  • 21
  • `/g` option in pattern matching in list context. Hats off! – Francisco R Mar 16 '11 at 09:42
  • It's not quite as obscure in a .pl file, but this is more or less the equivalent: `$/=0777;@ARGV=qw(terrible.pl);print+(@@=/count/g)+0while<>` Note: I'm saving my whitespace for another project. – LHMathies Mar 16 '11 at 15:32
  • @LHMathies I have a question like that: http://stackoverflow.com/questions/5402405/how-to-rewrite-of-one-line-code-or-less-line-code-in-command-line-of-this-code Can you check it, is it possible to rewrite it from command line? One answer was that: perl -e 'open A,";open B,";y/GCTA/CGAU/;map{print if$_=$p{$_}}/(...)/g' However I think that it may be more less. – kamaci Mar 23 '11 at 12:56
1

If you are trying to count how many times appears the word "count", this will work:

my $count=0;
open(INPUT,"<terrible.pl");
while (<INPUT>) {
    $count++ while ($_ =~ /count/g);
}
close(INPUT);
print "$count times\n";
Francisco R
  • 4,032
  • 1
  • 22
  • 37
0

I'm not actually sure what your example code is but you're almost there:

perl -e '$text = "lol wut foo wut bar wut"; $count = 0; $count++ while $text =~ /wut/g; print "$count\n";'

You can use the /g modifier to continue searching the string for matches. In the example above, it will return all instances of the word 'wut' in the $text var.

vicTROLLA
  • 1,554
  • 12
  • 15
0

You can probably use something like so:

my $fh = IO::File->new('test.txt', 'r') or die "$!\n";
my %words;
while (<$fh>) {
  for my $word (split / /) {
    $words{$word}++;
  }
}

That will give you an accurate count of every "word" (defined as a group of characters separated by a space), and store it in a hash which is keyed by the word with a value of the number of the word which was seen.

BadFileMagic
  • 701
  • 3
  • 7
  • I just need the get tme number of given word. My word to find the number of count is: "count". – kamaci Mar 15 '11 at 21:55
0

perdoc perlrequick has an answer. The term you want in that document is "scalar context".

Given that this appears to be a homework question, I'll point you at the documentation instead.

Devdas
  • 394
  • 1
  • 4
0

So, what are you trying to do? You want the number of times something appears in a block of text. You can use the Perl grep function. That will go through a block of text without needing to loop.

If you want an odd/even return value, you can use the modulo arithmetic function. You can do something like this:

if ($number % 2) {
   print "$number is odd\n"; #Returns a "1" or true
}
else {
   print "$number is even\n";  #Returns a "0" or false
}
David W.
  • 105,218
  • 39
  • 216
  • 337