Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate Character Frequency in Message using Perl

I am writing a Perl Script to find out the frequency of occurrence of characters in a message. Here is the logic I am following:

  • Read one char at a time from the message using getc() and store it into an array.
  • Run a for loop starting from index 0 to the length of this array.
  • This loop will read each char of the array and assign it to a temp variable.
  • Run another for loop nested in the above, which will run from the index of the character being tested till the length of the array.
  • Using a string comparison between this character and the current array indexed char, a counter is incremented if they are equal.
  • After completion of inner For Loop, I am printing the frequency of the char for debug purposes.

Question: I don't want the program to recompute the frequency of a character if it's already been calculated. For instance, if character "a" occurs 3 times, for the first run, it calculates the correct frequency. However, at the next occurrence of "a", since loop runs from that index till the end, the frequency is (actual freq -1). Similary for the third occurrence, frequency is (actual freq -2).

To solve this. I used another temp array to which I would push the char whose frequency is already evaluated.

And then at the next run of for loop, before entering the inner for loop, I compare the current char with the array of evaluated chars and set a flag. Based on that flag, the inner for loop runs.

This is not working for me. Still the same results.

Here's the code I have written to accomplish the above:

#!/usr/bin/perl

use strict;
use warnings;

my $input=$ARGV[0];
my ($c,$ch,$flag,$s,@arr,@temp);

open(INPUT,"<$input");

while(defined($c = getc(INPUT)))
{
push(@arr,$c);
}

close(INPUT);

my $length=$#arr+1;

for(my $i=0;$i<$length;$i++)
{
$count=0;
$flag=0;
$ch=$arr[$i];
foreach $s (@temp)
{
    if($ch eq $s)
    {
        $flag = 1;
    }
}
if($flag == 0)
{
for(my $k=$i;$k<$length;$k++)
{
    if($ch eq $arr[$k])
    {
        $count = $count+1;
    }
}
push(@temp,$ch);
print "The character \"".$ch."\" appears ".$count." number of times in the         message"."\n";
}
}
like image 700
Neon Flash Avatar asked Dec 22 '22 07:12

Neon Flash


1 Answers

You're making your life much harder than it needs to be. Use a hash:

my %freq;

while(defined($c = getc(INPUT)))
{
  $freq{$c}++;
}

print $_, " ", $freq{$_}, "\n" for sort keys %freq;

$freq{$c}++ increments the value stored in $freq{$c}. (If it was unset or zero, it becomes one.)

The print line is equivalent to:

foreach my $key (sort keys %freq) {
  print $key, " ", $freq{$key}, "\n";
}
like image 81
Mat Avatar answered Dec 29 '22 21:12

Mat