This repository was archived by the owner on May 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yabuuchi
committed
Jan 21, 2015
1 parent
f09ae17
commit e33a9ea
Showing
10 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
use Data::Dumper; | ||
use feature ':5.10'; | ||
|
||
sub calc{ | ||
my ($first,$second) = @_; | ||
if($first =~ m|^\d+$| && $second =~ m/^\d+$/){ | ||
my %result = ( | ||
add => $first + $second, | ||
sub => $first - $second, | ||
mol => $first * $second, | ||
div => $first / $second, | ||
mod => $first % $second, | ||
); | ||
return \%result; | ||
}else{ | ||
return undef; | ||
} | ||
} | ||
|
||
print Dumper calc(9,6); | ||
print Dumper calc('a','b'); | ||
|
||
sub result_dump{ | ||
my $result = shift; | ||
for my $key (keys %$result){ | ||
print $key."=>".$result->{$key}.",\n"; | ||
} | ||
return; | ||
} | ||
|
||
# result_dump(calc(9,6)); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
use Data::Dumper; | ||
use feature ':5.10'; | ||
|
||
##################### | ||
##calc_string練習問題 | ||
###1.引数として与えられた文字列が、数値A 演算子 数値Bという文字列であれば、その式を計算して結果を返す関数calc_stringを書いてみましょう | ||
###2.関数calc_stringとwhile文を使って、Ctrlキーとdキーを押すまでの間、標準入力から文字列を受け取り、文字列に書かれた式を計算するようなコードを書いてみましょう | ||
|
||
sub calc_string{ | ||
my $str = shift; | ||
return "undifined_if!" if !($str); | ||
return "undifined_unless" unless ($str); | ||
return "undifined_defined" if !defined($str); | ||
my @strings = split(/ /,$str); | ||
return 'first not num!!' unless $strings[0] =~ m/^[-]?\d+$/; | ||
return 'not set Operator' unless $strings[1] =~ m/(\+|\-|\*|\/|%)/; | ||
return 'second not num!!' unless $strings[2] =~ /^[-]?\d+$/; | ||
my $result = ''; | ||
$result = $strings[0] + $strings[2] if $strings[1] eq '+'; | ||
$result = $strings[0] - $strings[2] if $strings[1] eq '-'; | ||
$result = $strings[0] * $strings[2] if $strings[1] eq '*'; | ||
$result = $strings[0] / $strings[2] if $strings[1] eq '/'; | ||
$result = $strings[0] % $strings[2] if $strings[1] eq '%'; | ||
return $result; | ||
} | ||
|
||
while(my $input = <STDIN>) { | ||
chomp $input; | ||
my $result = calc_string($input); | ||
print "$result\n"; | ||
} | ||
# print calc_string('')."\n"; | ||
# print calc_string('1 +1')."\n"; | ||
# print calc_string('1 + 1')."\n"; | ||
# print calc_string('3 - 1')."\n"; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
use Data::Dumper; | ||
use feature ':5.10'; | ||
|
||
######################## | ||
##Perl入学式 #4 復習問題 | ||
###3.$data に人物名と好きな食べ物を (スペース):(スペース) 区切りで与えています. | ||
###この変数から食べ物が何回出現したかカウントして表示させてください. | ||
|
||
my $data = q{ | ||
papix : sushi | ||
moznion : soba | ||
boolfool : sushi | ||
macopy : sushi | ||
}; | ||
|
||
my @lines = split "\n", $data; | ||
my %foods_count; | ||
for my $line (@lines){ | ||
if($line =~ m/(\w+)\s:\s(\w+)/){ | ||
$foods_count{$2}++; | ||
} | ||
} | ||
print Dumper \%foods_count; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# #!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
|
||
package PerlBeginners; | ||
sub perllevel{ | ||
my $level = shift; | ||
unless($level =~ m/^[1-9]$|10/){ | ||
return "no set level!!" | ||
} | ||
my % my_level = ( | ||
1 => "Perl 関係の書籍や資料を何も読んでいない。Perl がプログラミング言語だということは知っているが、それ以外のことは何も知らない。他人の書いたPerl プログラムを実行できるので、プログラムの一部を編集することでプログラムの動作の一部(出力される文字列の内容など)を変更できることを知っている。プログラムのほかの部分に変更を加えてもなぜうまくいかないのか理解していない。この言語に合うメンタルモデルを持っていないので、Perl の構文をCOBOL とC++ のような他の言語のものとは区別できていない。", | ||
2 => "基本ブロック構造の構文を理解しているが、JavaScriptのような言語に似ているという程度の認識にとどまっている。ブロックがある種のスコープ効果を生じさせるという理解はあるものの、レキシカル変数のことは知らないし、use strict や usewarningsに出会ったこともない。条件の意味を変更することができ、基本的な算術演算子と論理演算子条件を使用できる。必要とするすべてのことを、他人の書いたプログラムに若干の変更を加えることで達成できると思っている。", | ||
3 => "プログラムをゼロから作成したいが、ある種の教育が必要だとわかっている。Perl を学ぶのに適した書籍を推薦して欲しいと思っている。このレベルのプログラマの中には、ラクダ本[WALL00] をチュートリアルだと思って手に入れ、その内容を読破しようとして、重いトラウマに苦しむ者がいるかもしれない。", | ||
# | ||
# . | ||
# . | ||
# . | ||
# . | ||
# . | ||
# . | ||
# . | ||
# | ||
10 => "Perl のobfuscation コンテストや「golf」コンテスト*3 に参加する。たとえば、普通のプログラマなら1 つのプログラムを丸ごと必要とするような関数を、埋め込みコードを使って単一の正規表現で実装してしまう。Perl コアにパッチを送ったり、新しいメジャーなモジュールを寄贈したりすることで、Perl コミュニティで有名人になるかもしれない" | ||
); | ||
return $my_level{$level}; | ||
} | ||
1; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
use Data::Dumper; | ||
use feature ':5.10'; | ||
|
||
package PerlEntrance{ | ||
sub tokyo{ | ||
return "Tokyo!"; | ||
} | ||
sub osaka{ | ||
return "Osaka!"; | ||
} | ||
} | ||
1; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
use Data::Dumper; | ||
use feature ':5.10'; | ||
|
||
package Yapc; | ||
sub year{ | ||
return "2015"; | ||
} | ||
sub month{ | ||
return '08' | ||
} | ||
sub day{ | ||
return '20','21','22'; | ||
} | ||
|
||
1; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
use Data::Dumper; | ||
use feature ':5.10'; | ||
|
||
################### | ||
##mapを使用した例題 | ||
|
||
my @array = (1..10); | ||
my @array2 = (); | ||
for my $val(@array){ | ||
push @array2,$val*2; | ||
} | ||
|
||
my @array3 = map{$_ * 2}@array; | ||
say Dumper @array3; | ||
|
||
my @strs = ('aa','bb c','c c'); | ||
say @strs; | ||
|
||
my @forma_str = map{$_ =~ s/\s//g;$_}@strs; | ||
print "@forma_str"."\n"; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
use Data::Dumper; | ||
use feature ':5.10'; | ||
|
||
my @files = qw/foo.pl bar.pm baz.rb qux.py/; | ||
|
||
sub map_back{ | ||
my @files = @_; | ||
my @format = (); | ||
@format = map{$_ =~ s/$/.bak/g;$_;}@files; | ||
return @format; | ||
} | ||
say Dumper map_back(@files); | ||
|
||
sub grep_pl_and_pm{ | ||
my @files = @_; | ||
my @get = (); | ||
@get = grep{$_ =~ /pm|pl/}@files; | ||
return @get; | ||
} | ||
say Dumper grep_pl_and_pm(@files); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
use lib "lib"; | ||
use PerlEntrance; | ||
|
||
print PerlEntrance::tokyo(); | ||
print PerlEntrance::osaka(); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/usr/bin/env perl | ||
|
||
use strict; | ||
use warnings; | ||
use lib "lib"; | ||
use PerlBeginners; | ||
|
||
################## | ||
##練習問題 | ||
###PerlBeginnersというモジュールを作って、その中にperllevelというサブルーチンを作りましょう | ||
###perllevelは1から10の整数の引数を取ります | ||
###引数をレベルと解釈して、上記のように使います | ||
###Perlレベルは以下のブログに掲載されています。何らかの形でモジュール内に保持しましょう | ||
|
||
print PerlBeginners::perllevel(10); | ||
print PerlBeginners::perllevel('a'); |