Skip to content

Instantly share code, notes, and snippets.

@yaasita
Created April 26, 2025 14:09
Show Gist options
  • Save yaasita/671be7265d11b60f37fb7dbc0df11bcf to your computer and use it in GitHub Desktop.
Save yaasita/671be7265d11b60f37fb7dbc0df11bcf to your computer and use it in GitHub Desktop.
use strict;
use warnings;
# ファイルを開く
my $file = 'rakuten.csv';
open my $fh, '<', $file or die "Could not open '$file' $!";
# 数字のカウント用ハッシュ
my %first_digit_count;
my $total_count = 0;
# ファイルを1行ずつ読み込む
while (my $line = <$fh>) {
chomp $line;
# 金額列を抽出(9番目の列)
my @fields = split /,/, $line;
my $amount = $fields[8];
# 金額が数字で始まる場合、最初の数字を取得
if ($amount =~ /^(\d)/) {
$first_digit_count{$1}++;
$total_count++;
}
}
close $fh;
# 各数字の割合を計算して出力
print "First Digit Analysis:\n";
foreach my $digit (1..9) {
my $count = $first_digit_count{$digit} // 0;
my $percentage = ($count / $total_count) * 100;
printf "%d: %.2f%%\n", $digit, $percentage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment