たくさんの検索を行いましたが、私が望んでいたものは何もありませんでした。Perl初心者はこちら。
私は、データ行にきちんと整理されたテキスト ファイルを既に持っています。私が興味を持っている 2 つの文字列は、"hello" と "goodbye" だとします。最初の行を見て、「こんにちは」と「さようなら」が何回出現するかをカウントする簡単な Perl スクリプトを書きたいと思います。次に、次の行に移動してカウントを行い、以前のカウントに追加します。したがって、スクリプトの最後までに、ファイル内の各文字列の合計カウント数を出力できます。行ごとのアプローチが重要な理由は、いくつかのカウントを使用して、両方の単語が同じ行にある回数を出力できるようにするためです。その他、行に「こんにちは」が1回含まれているが「さようなら」が複数回含まれている回数など。
これまでのところ、私は考えています:
#!/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.
おっしゃる通り、私は初心者です。各行の出現をカウントする方法についても混乱しています。たとえ、上に挙げたさまざまな条件をすべて把握できると確信していたとしても. 配列を使用する必要がありますか? ハッシュ?または、私が何を望んでいるのかを考慮して、これに完全に間違った方向にアプローチしましたか。これらの変数の後にコメントとしてリストしたさまざまな条件を持つ行の数を数える必要があります。どんな助けでも大歓迎です!