Last active
December 21, 2015 16:09
-
-
Save zaneli/6331985 to your computer and use it in GitHub Desktop.
Perl 入学式 #3 用
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
print "問題1.\n"; | |
my @array = ('Alice', 'Bob'); | |
unshift @array, "Amon2"; | |
push @array, "Catalyst"; | |
for my $elem (@array) { | |
print "$elem\n"; | |
} | |
print "\n問題2.\n"; | |
my @array = ('Alice', 'Bob'); | |
my $head = shift @array; | |
print $head . "\n"; | |
print "\n問題3.\n"; | |
my @array = ('Alice', 'Bob'); | |
my $last = pop @array; | |
print $last . "\n"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
my @array1 = qw(0123 123 XXX); | |
my $phonenum = join '-', @array1; | |
print "$phonenum\n"; | |
my $array2 = "/usr/bin/env perl"; | |
my @path = split /\//, $array2; | |
for my $elem (@path) { | |
print "$elem\n"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
chomp(my $num = <STDIN>); | |
if ($num % 2 == 0) { | |
print $num . " is even.\n"; | |
} else { | |
print $num . " is odd.\n"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
my %hash = ( | |
name => "zaneli", | |
sex => "male", | |
food => "焼き肉" | |
); | |
print Dumper(%hash); | |
use Data::Dumper; # 呼び出す場所より後ろでもいいんだ… |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Data::Dumper; | |
my %hash = ( | |
name => "zaneli", | |
sex => "male", | |
food => "焼き肉" | |
); | |
for my $key (keys %hash) { | |
print "$key : $hash{$key}\n"; | |
} | |
delete $hash{sex}; | |
print Dumper(%hash); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
my $sum = 0; | |
for my $i (1 .. 100) { | |
$sum += $i; | |
} | |
print $sum . "\n"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use Data::Dumper; | |
my $tarou_ref = { | |
name => "tarou", | |
foods => [qw/ラーメン 寿司 うどん/] | |
}; | |
my $jirou_ref = { | |
name => "jirou", | |
foods => [qw/寿司 カレー 焼き肉/] | |
}; | |
my $saburou_ref = { | |
name => "saburou", | |
foods => [qw/寿司 そば カレー/] | |
}; | |
my @people = ($tarou_ref, $jirou_ref, $saburou_ref); | |
my %vote = (); | |
for my $person (@people) { | |
for my $foods ($person -> {foods}) { | |
for my $food (@$foods) { | |
# このif文は要らないはずだけど、せっかく exists を習ったので。 | |
if (exists $vote{$food}) { | |
$vote{$food} += 1; | |
} else { | |
$vote{$food} = 1; | |
} | |
} | |
} | |
} | |
print Dumper(%vote); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment