使用 Perl 计算消息中的字符频率

发布于 2024-12-10 08:12:29 字数 1249 浏览 0 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

水中月 2024-12-17 08:12:29

你让你的生活变得比需要的更加困难。使用哈希:

my %freq;

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

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

$freq{$c}++ 递增存储在 $freq{$c} 中的值。 (如果未设置或为零,则变为 1。)

打印行相当于:

foreach my $key (sort keys %freq) {
  print $key, " ", $freq{$key}, "\n";
}

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";
}
想你的星星会说话 2024-12-17 08:12:29

如果您想对整个文件进行单个字符计数,请使用其他人发布的任何建议方法。如果您想要计算所有发生的次数
文件中的每个字符然后我建议:

#!/usr/bin/perl

use strict;
use warnings;

# read in the contents of the file
my $contents;
open(TMP, "<$ARGV[0]") or die ("Failed to open $ARGV[0]: $!");
{
    local($/) = undef;
    $contents = <TMP>;
}
close(TMP);

# split the contents around each character
my @bits = split(//, $contents);

# build the hash of each character with it's respective count
my %counts = map { 
    # use lc($_) to make the search case-insensitive
    my $foo = $_; 

    # filter out newlines
    $_ ne "\n" ? 
        ($foo => scalar grep {$_ eq $foo} @bits) :
        () } @bits;

# reverse sort (highest first) the hash values and print
foreach(reverse sort {$counts{$a} <=> $counts{$b}} keys %counts) {
    print "$_: $counts{$_}\n";
}

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:

#!/usr/bin/perl

use strict;
use warnings;

# read in the contents of the file
my $contents;
open(TMP, "<$ARGV[0]") or die ("Failed to open $ARGV[0]: $!");
{
    local($/) = undef;
    $contents = <TMP>;
}
close(TMP);

# split the contents around each character
my @bits = split(//, $contents);

# build the hash of each character with it's respective count
my %counts = map { 
    # use lc($_) to make the search case-insensitive
    my $foo = $_; 

    # filter out newlines
    $_ ne "\n" ? 
        ($foo => scalar grep {$_ eq $foo} @bits) :
        () } @bits;

# reverse sort (highest first) the hash values and print
foreach(reverse sort {$counts{$a} <=> $counts{$b}} keys %counts) {
    print "$_: $counts{$_}\n";
}
江南烟雨〆相思醉 2024-12-17 08:12:29

我不明白你要解决的问题,所以我提出了一种更简单的方法来计算字符串中的字符:

$string = "fooooooobar";
$char = 'o';
$count = grep {$_ eq $char} split //, $string;
print $count, "\n";

这会打印 $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:

$string = "fooooooobar";
$char = 'o';
$count = grep {$_ eq $char} split //, $string;
print $count, "\n";

This prints the number of $char occurrences in $string (7).
Hope this helps to write a more compact code

戏剧牡丹亭 2024-12-17 08:12:29

作为单行:

perl -F"" -anE '$h{$_}++ for @F; END { say "$_ : $h{$_}" for keys %h }' foo.txt

As a one-liner:

perl -F"" -anE '$h{$_}++ for @F; END { say "$_ : $h{$_}" for keys %h }' foo.txt
断念 2024-12-17 08:12:29

更快的解决方案:

@result = $subject =~ m/a/g; #subject is your file

print "Found : ", scalar @result, " a characters in file!\n";

当然,您可以将变量放在“a”的位置,或者更好地为您想要计算出现次数的任何字符执行此行。

Faster solution :

@result = $subject =~ m/a/g; #subject is your file

print "Found : ", scalar @result, " a characters in file!\n";

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文