做了很多搜索,没有我想要的。Perl 菜鸟在这里。
我有一个文本文件已经整齐地组织成数据行。假设我感兴趣的两个字符串是“hello”和“goodbye”。我想编写一个快速的 Perl 脚本,它会查看第一行并计算“hello”和“goodbye”出现的次数。然后它将转到下一行并进行计数,并添加到先前的计数中。因此,在脚本结束时,我可以打印文件中每个字符串的总计数。逐行方法很重要的原因是因为我想使用几个计数,所以我可以打印两个单词在同一行的次数,一行只包含一个单词而不是其他,一行包含“hello”一次但“goodbye”多次等的次数。真的
到目前为止,我在想:
#!/usr/bin/perl
use strict; use warnings;
die etc (saving time by not including it here)
my $word_a = "hello";
my $word_b = "goodbye";
my $single_both = 0; # Number of lines where both words appear only once.
my $unique_hello = 0; # Number of lines where only hello appears, goodbye doesn't.
my $unique_goodbye = 0; # Number of lines where goodbye appears, hello doesn't.
my $one_hello_multiple_goodbye = 0; # Number of lines where hello appears once and goodbye appears multiple times.
my $one_goodbye_multiple_hello = 0; # Number of lines where goodbye appears once and hello appears multiple times.
my $multiple_both = 0; = # Number of lines where goodbye and hello appear multiple times.
while (my $line = <>) {
Magic happens here
};
# then the results for each of those variables can be printed at the end.
正如我所说,我是一个菜鸟。我对如何计算每行中的出现次数感到困惑。即使我知道我确信我可以找出上面列出的所有不同条件。我应该使用数组吗?哈希?或者考虑到我想要什么,我是否在完全错误的方向上处理了这个问题。我需要计算具有不同条件的行数,这些条件我在这些变量之后列为注释。非常感谢任何帮助!