読者です 読者をやめる 読者になる 読者になる

sironekotoroの日記

Perl と Mac の初心者の備忘録

Perl言語プログラミングレッスン[入門編] 第5章

5-3

use strict;
use warnings;
use utf8;

my $kan_num = "五";

my %hash = (
    〇 => 0,
    壱 => 1,
    二 => 2,
    三 => 3,
    四 => 4,
    五 => 5,
    六 => 6,
    七 => 7,
    八 => 8,
    九 => 9,
    拾 => 10,
);

print $hash{"$kan_num"} . "\n";

#

my @kan = ("〇","壱","二","三","四","五","六","七","八","九","拾");
for my $i (0..10){
    if ($kan[$i] eq $kan_num){
        print "$i\n";
    }
}

5-4

use strict;
use warnings;

my $work;

foreach (<DATA>) {
    if ( $_ =~ /2000/ ) {
    }
    elsif ( $_ =~ /(.+?) (\d.*)/ ) {
        $work->{$1} += $2;
    }
}

foreach ( keys $work ) {
    printf( "%-8s %3d時間 %3d分\n", "$_", $work->{$_} / 60,
        "$work->{$_}" );
}

__DATA__
2000-01-01
red 10
purple 35
green 22
2000-01-12
red 35
green 21
2000-01-15
purple 40
white 5
2000-01-31
red 10
purple 5
yellow 5