再帰的トラバーサルや数学の問題の解決などを行う例を含む多くの Scala チュートリアルを目にします。日常のプログラミング生活の中で、コーディング時間のほとんどは、文字列操作、データベース クエリ、日付操作などのありふれたタスクに費やされていると感じています。次の Perl スクリプトの Scala バージョンの例を示してくれる人はいますか?
#!/usr/bin/perl
use strict;
#opens a file with on each line one word and counts the number of occurrences
# of each word, case insensitive
print "Enter the name of your file, ie myfile.txt:\n";
my $val = <STDIN>;
chomp ($val);
open (HNDL, "$val") || die "wrong filename";
my %count = ();
while ($val = <HNDL>)
{
chomp($val);
$count{lc $val}++;
}
close (HNDL);
print "Number of instances found of:\n";
foreach my $word (sort keys %count) {
print "$word\t: " . $count{$word} . " \n";
}
要約すれば:
- ファイル名を尋ねる
- ファイルを読み取ります (1 行に 1 単語が含まれます)
- 行末をなくす ( cr, lf または crlf)
- 単語を小文字にする
- 単語の増分カウント
- アルファベット順にソートされた各単語とその数を出力します
ティア