使用 Perl 计算消息中的字符频率
我正在编写一个 Perl 脚本来找出消息中字符出现的频率。这是我遵循的逻辑:
- 使用 getc() 从消息中一次读取一个字符并将其存储到一个数组中。
- 从索引 0 开始运行 for 循环,直到该数组的长度。
- 此循环将读取数组的每个字符并将其分配给临时变量。
- 运行另一个嵌套在上面的 for 循环,该循环将从正在测试的字符的索引运行到数组的长度。
- 使用此字符和当前数组索引字符之间的字符串比较,如果它们相等,则计数器会递增。
- 完成内部 For 循环后,我将打印字符的频率以进行调试。
问题:如果已经计算过字符的频率,我不希望程序重新计算该字符的频率。例如,如果字符“a”出现 3 次,则第一次运行时,它会计算出正确的频率。然而,在下一次出现“a”时,由于循环从该索引运行到结束,因此频率为(实际频率-1)。与第三次出现类似,频率是(实际频率-2)。
为了解决这个问题。我使用了另一个临时数组,将频率已经评估过的字符推送到其中。
然后在下一次运行 for 循环时,在进入内部 for 循环之前,我将当前字符与评估的字符数组进行比较并设置一个标志。基于该标志,内部 for 循环运行。
这对我不起作用。结果还是一样。
这是我为完成上述任务而编写的代码:
#!/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";
}
}
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";
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你让你的生活变得比需要的更加困难。使用哈希:
$freq{$c}++
递增存储在$freq{$c}
中的值。 (如果未设置或为零,则变为 1。)打印行相当于:
You're making your life much harder than it needs to be. Use a hash:
$freq{$c}++
increments the value stored in$freq{$c}
. (If it was unset or zero, it becomes one.)The print line is equivalent to:
如果您想对整个文件进行单个字符计数,请使用其他人发布的任何建议方法。如果您想要计算所有发生的次数
文件中的每个字符然后我建议:
If you want to do a single character count for the whole file then use any of the suggested methods posted by the others. If you want a count of all the occurances
of each character in a file then I propose:
我不明白你要解决的问题,所以我提出了一种更简单的方法来计算字符串中的字符:
这会打印 $string (7) 中 $char 出现的次数。
希望这有助于编写更紧凑的代码
I don´t understand the problem you are trying to solve, so I propose a more simple way to count the characters in a string:
This prints the number of $char occurrences in $string (7).
Hope this helps to write a more compact code
作为单行:
As a one-liner:
更快的解决方案:
当然,您可以将变量放在“a”的位置,或者更好地为您想要计算出现次数的任何字符执行此行。
Faster solution :
Of course you can put a variable in the place of 'a' or even better execute this line for whatever characters you want to count the occurrences.