Skip to content

Instantly share code, notes, and snippets.

@tomcha
Created December 2, 2016 12:55
Show Gist options
  • Save tomcha/f5358eee1ad42c4549a21992e33e98c3 to your computer and use it in GitHub Desktop.
Save tomcha/f5358eee1ad42c4549a21992e33e98c3 to your computer and use it in GitHub Desktop.
Perl入学式 #3 復習問題 score.pl
use strict;
use warnings;
binmode STDOUT, ":utf8";
use Data::Dumper;
my $alice = {
name => 'Alice',
country => 'England',
perl => 60,
python => 80,
ruby => 80,
php => 50,
binary => 30,
};
my $bob = {
name => 'bob',
country => 'America',
perl => 40,
python => 10,
ruby => 20,
php => 30,
binary => 50,
};
my $carol = {
name => 'Carol',
country => 'England',
perl => 100,
python => 70,
ruby => 80,
php => 50,
binary => 50,
};
my $dave = {
name => 'Dave',
country => 'Canada',
perl => 10,
python => 11,
ruby => 1,
php => 100,
binary => 100,
};
my $ellen = {
name => 'Ellen',
country => 'America',
perl => 1,
python => 15,
ruby => 0.5,
php => 60,
binary => 0.01,
};
my @people = ($alice, $bob, $carol, $dave);
my @people2 = @people;
my @languages = qw(perl python ruby php binary);
#問1
for my $personaldata (@people){
$personaldata->{sum} = 0;
for my $lang (@languages){
$personaldata->{sum} += $personaldata->{$lang};
}
}
print Dumper @people;
#問2
my $average = {};
for my $personaldata (@people){
my $three_score = $personaldata->{ruby} + $personaldata->{perl} + $personaldata->{python};
$average->{$personaldata->{name}} = ($three_score / 3);
}
print Dumper $average;
#問3
for my $personaldata (@people){
print "$personaldata->{name}\n";
for my $lang (@languages){
print " $lang : ";
my $starcount = int($personaldata->{$lang} / 20);
for (my $i = 0; $i < $starcount; $i++){
print '*';
}
print "\n";
}
}
#問4
my $highscore = {};
for my $personaldata (@people){
my $perlscore = $personaldata->{perl};
if ($perlscore >= 60){
my $country = $personaldata->{country};
push @{$highscore->{$country}}, $personaldata->{name};
}
}
print Dumper $highscore;
#問5
my @strseeds;
push @strseeds, "[";
for my $personaldata (@people){
my @keys = keys %$personaldata;
push @strseeds, " {";
for my $key (@keys){
if ($key eq 'name' || $key eq 'country'){
push @strseeds ," \"$key\":\"$personaldata->{$key}\"";
}
else{
push @strseeds, " \"$key\":$personaldata->{$key}";
}
}
push @strseeds, " },";
}
$strseeds[-1] = ' }' if $strseeds[-1] eq ' },';
push @strseeds, "]";
print join "\n", @strseeds;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment