my $score = 75; if ($score >= 80) { print("OK"); } elsif ($score >= 40) { print ("not ok..."); } else { print("take the test again") }
1行で書く場合
my $score = 75; print("OK!") if ($score >= 60);
my $a = 10; my $b = 20; my $max = $a > $b ? $a : $b;
ソフトウェアエンジニアの技術ブログ:Software engineer tech blog
随机应变 ABCD: Always Be Coding and … : хороший
my $score = 75; if ($score >= 80) { print("OK"); } elsif ($score >= 40) { print ("not ok..."); } else { print("take the test again") }
1行で書く場合
my $score = 75; print("OK!") if ($score >= 60);
my $a = 10; my $b = 20; my $max = $a > $b ? $a : $b;
my @sales = (300, 150, 200); # @sales $#sale: 末尾の添字 print @sales[0..$#sales];
key and value
my %sales = ("tanaka"=>300, "suzuki"=>150, "ohira"=>200); print $sales{"tanaka"};
条件分岐
my $score = 85; if ($score >= 80) { print("ok!"); }
vagrant@vagrant-ubuntu-trusty-64:~/app$ perl hello.pl
ok!
my $score = 85; my $name = "Luke"; if ($name eq "Luke") { print("I am your father!"); }
vagrant@vagrant-ubuntu-trusty-64:~/app$ perl hello.pl
I am your father!
ああああああ、何やってんだろう。
my $score = 85; my $name = "Luke"; if (($name eq "Luke") &&($score >= 80)) { print("I am your father!"); }
my $x = 10; my $y = 10.75; my $z = 12_2425_241; my $xx = 10 * 10; my $yy = 10 % 3; my $zz = 2 ** 3; $x++; $x--; $x += 5;
my $a = "he\tllo"; # 特殊文字(\n, \t)が使える、変数展開がされる my $b = 'he\tllo'; print $a; print $b;
hellohellovagrant@vagrant-ubuntu-trusty-64:~/app$ perl hello.pl
he llohe\tllo
こうも書ける。
my $name = "tom"; my $a = "hello, $name\n"; # 特殊文字(\n, \t)が使える、変数展開がされる my $b = 'hello'; print $a; print $b;
# . x 文字の連結
配列変数
my @sales = (150, 200, 300); my @colors = ('red', 'green'); my @mixed = (150, 'red', 1.5); print @sales; my @inc = (1..100); print @inc;
配列の読み出し
my @sales = (300, 150, 200); print $sales[1];
my @sales = (300, 150, 200); print @sales[0..2];
use strict; use warnings; print("hello world\n");
vagrant@vagrant-ubuntu-trusty-64:~/app$ perl hello.pl
hello world
my $msg; $msg = "Hello World\n"; print $msg;
こうも書ける。
my $msg = "Studying Perl\n"; print $msg;
[vagrant@localhost perl]$ vi hello_world.pl [vagrant@localhost perl]$ ls hello.pl hello_world.pl test.dat [vagrant@localhost perl]$ chmod 755 hello_world.pl [vagrant@localhost perl]$ ls hello.pl hello_world.pl test.dat [vagrant@localhost perl]$ ./hello_world.pl hello, world! [vagrant@localhost perl]$ perl -e 'print "hello_world\n";' hello_world
さーどうしようか。。とりあえず思い出すまで基礎かな。
[vagrant@localhost ~]$ perl -v This is perl, v5.10.1 (*) built for x86_64-linux-thread-multi Copyright 1987-2009, Larry Wall Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit. Complete documentation for Perl, including FAQ lists, should be found on this system using "man perl" or "perldoc perl". If you have access to the Internet, point your browser at http://www.perl.org/, the Perl Home Page.
use strict; use warnings; print "hello world\n";
スカラー変数
my $x = 123_234_456
文字列
my $a = "he\tllo\n" #特殊文字、変数展開 my $a = 'hello'
配列
my @sales = (150, 200, 300); print @sales; my @sales = (150, 200, 300); print $sales[1];
ハッシュ変数
my %sales = ("tanaka"=>150, "suzuki"=>200, "yamada"=>300); print $sales{"tanaka"}
ループ
my $i = 0; while ($i < 10){ print "i = $i\n"; $i++; }[/perl] 配列ループ [prel] my @colors = qw(red green blue orange pink); foreach my $color(@colors){ print "color = $color\n"; }[/perl] ハッシュループ処理 [perl]my %sales = ("tanaka"=>150, "suzuki"=>300, "ohira"=>200); foreach my $key(keys(%sales)){ print "sales for $key is $sales{$key}\n"; }
ファイルの読み込み
open(my $in, "<", "test.dat") or die("could not open file."); while(<$in>){ print $_; } close($in);
置換
$_ =~ s/abc/ABC;
正規表現
while(<$in>){ $_ =~ s/abc/ABC/; if ($_ =~ /[a-z]/){ print $_; } }
サブルーチン
sub max{ my $max = $_[0]; if ($_[1]> $max){ $max = $_[1]; } return $max; } print max(2, 8);