ホームに戻る
 Perlのサンプル

# perl のサンプル

#
# 実行結果
#
#Hello, World!
#abcdefghi
#num = 123123
#$num = 456
#1c8
#012345
#3
#6
#6
#num < 3
#num < 3 && num > 0
#num == 01
#2
#3
#5
#7
#2
#3
#5
#7
#2,3,5,7
#1
#3
#5
#7
#a:b:c:d:e:f
#abcdefg
#g
#a
#0bcdef
#10AB50
#203040
#-13:0:10:1000:9
#-13:0:9:10:1000
#1000:10:9:0:-13
#2
#7
#ACB
#257
#ABC
#257
#A:2
#C:5
#B:7
#
#2
#abc
#def
#ghiabcdefghi123
#0
#1
#2
#3
#4
#5
#6
#7
#8
#9
#
#AAA
#cd is match
#ab is match
#def is match
#abcdef is match
#b$ is match
#abcdfe is match
#Bdef is match
#Adef is match
#Adef is match
#8def is match
#color is match
#gd is match
#good is match
#aHuqk66t5 is match
#abbbb is match
#abcbcbcde is match
#This is a pen is match
#1\ is match
#333 is match
#300 is match
#200, 100, 300 is match
#200 is match
#100 is match
#300 is match
#200
#100
#300
#This is an orange
#This is an "apple"
#This is an apPle
#This is an
#This is an Apple
#There are biscuits AND candies
#There are biscuits and cANDies
#ABC is match
#abc is match
#ABC is match
#tHIS IS AN APPLE
#Tijt jt bo bqqmf a
#abcdef
#cdef
#7
#1: hop
#2: step
#3: jump

use strict;
use warnings;

# Hello,World!

print "Hello, World!\n";

# 文字列と連結

my $str1 = "abc";
my $str2 = "def";
my $str3 = $str1 . $str2;
$str3 .= "ghi";

print $str3, "\n";

# 数字から文字へ

my $num = 123;

print "num = $num";

print $num, "\n";

# 文字から数字へ

$num = "456";

print '$num', " = $num\n";

printf "%x\n", $num;

# 配列

my @array = (0, 1, 2, 3, 4, 5);

print @array, "\n";

print $array[3], "\n";

print $#array + 1, "\n";

my $array_length = @array;

print $array_length, "\n";

# if 文

$num = 1;

if($num < 3){
  print "num < 3\n";
}
elsif($num == 3){
  print "num == 3\n";
}
else{
  print "num > 3\n";
}

if($num < 3 and $num > 0 or $num == -1){
  print "num < 3 && num > 0\n";
}

print "num == 0$num\n" if($num < 10);

unless($num == 1){
  print "num < 3 && num > 0\n";
}

# 配列の操作

my @a = (2, 3, 5, 7);

foreach my $x (@a){
  print $x, "\n";
}

foreach (@a){
  print $_, "\n";
}

print join(',', @a), "\n";

my $s = "1,3,5,7";

my @b = split(/,/, $s);

foreach (@b){
  print $_, "\n";
}

$s = "abcdef";

my @c = split(//, $s);
$s = join(':', @c);

print $s, "\n";

push(@c, "g");

print @c, "\n";

$s = pop(@c);

print $s, "\n";

$s = shift(@c);

print $s, "\n";

unshift(@c ,"0");

print @c, "\n";

my @d = (10, 20, 30, 40, 50);
my @e = ("A", "B");

my @f = splice(@d, 1, 3, @e);

print @d, "\n";
print @f, "\n";

my @g = (9, -13, 10, 0, 1000);

my @h = sort(@g);

print join(':', @h), "\n";

@h = sort { $a <=> $b } (@g);

print join(':', @h), "\n";

@h = reverse(@h);

print join(':', @h), "\n";

# hash

my %hash = ('A', 2, 'B', 3, 'C', 5);

print $hash{'A'}, "\n";

$hash{'B'} = 7;

print $hash{'B'}, "\n";

foreach my $key (keys(%hash)){
  print $key;
}

print "\n";

foreach my $value (values(%hash)){
  print $value;
}

print "\n";

foreach my $key (sort keys(%hash)){
  print $key;
}

print "\n";

foreach my $value (sort { $hash{$a} <=> $hash{$b} } keys(%hash)){
  print $hash{$value};
}

print "\n";

while(my($key, $value) = each(%hash)){
  print "$key:$value\n";
}

print "\n";

if(exists($hash{"A"})){
  print $hash{"A"}, "\n";
}

delete $hash{"B"};

undef %hash;

# 配列の操作

my $s1 = "abc\ndef\nghi\n";

chomp($s1); # 最後の改行を取り除く

print $s1;

for(split(/\n/,$s1)){
  print $_;
}

my @i = (1, 2, 3);

while(my $num = shift @i){
  print $num;
  last;
  print $num + 1;
}

while(my $num = shift @i){
  print $num;
  next;
  print $num + 1;
}

while(my $num = shift @i){
  print $num;
  # redo; # 1 を表示する無限ループ
  print $num + 1;
}

print "\n";

foreach my $i(0..9){
  print $i, "\n";
}

print "\n";

print "A" x 3, "\n";

# 正規表現

if("abcdef" =~ /cd/){
  print "$& is match\n";
}
else{
  print "no match";
}

if("abcdef" =~ /^ab/){
  print "$& is match\n";
}
else{
  print "no match";
}

if("abcdef" =~ /def$/){
  print "$& is match\n";
}
else{
  print "no match";
}

if("abcdef" =~ /^abcdef$/){
  print "$& is match\n";
}
else{
  print "no match";
}

if("ab\$cd" =~ /b\$/){
  print "$& is match\n";
}
else{
  print "no match";
}

if("abcdfe" =~ /ab..fe/){
  print "$& is match\n";
}
else{
  print "no match";
}

if("Bdef" =~ /[ABC]def/){
  print "$& is match\n";
}
else{
  print "no match";
}

if("Adef" =~ /[A-Za-z0-9]def/){
  print "$& is match\n";
}
else{
  print "no match";
}

if("Adef" =~ /[A-Za-z0-9]def/){
  print "$& is match\n";
}
else{
  print "no match";
}

if("8def" =~ /[^A-Z]def/){
  print "$& is match\n";
}
else{
  print "no match";
}

if("color" =~ /colou?r/){
  print "$& is match\n";
}
else{
  print "no match";
}

if("gd" =~ /go*d/){
  print "$& is match\n";
}
else{
  print "no match";
}

if("good" =~ /go+d/){
  print "$& is match\n";
}
else{
  print "no match";
}

if("aHuqk66t5" =~ /.+/){
  print "$& is match\n";
}
else{
  print "no match";
}

if("abbbbbcde" =~ /^.b{2,4}/){
  print "$& is match\n";
}
else{
  print "no match";
}

if("abcbcbcde" =~ /a(bc)*de/){
  print "$& is match\n";
}
else{
  print "no match";
}

if("This is a pencil" =~ /This is a (pen|pencil)/){
  print "$& is match\n";
}
else{
  print "no match";
}

if("1\\" =~ /\d\D*/){
  print "$& is match\n";
}
else{
  print "no match";
}

if("333" =~ /^(\d)\1+$/){
  print "$& is match\n";
}
else{
  print "no match";
}

my $sss = "The price is 300 yen";

if($sss =~ /\d+/){
  print "$& is match\n";
}
else{
  print "no match";
}

$sss = "200,100,300";

if($sss =~ /(\d+),(\d+),(\d+)/){
  print "$1, $2, $3 is match\n";
}
else{
  print "$1, $2, $3 is no match";
}

$sss = "200,100:300";

while($sss =~ /\d+/g){
  print "$& is match\n";
}

my @array_sss = ($sss =~ /\d+/g);

foreach (@array_sss){
  print $_, "\n";
}

$sss = "This is an apple";

$sss =~ s/apple/orange/;

print $sss, "\n";

$sss = "This is an apple";

$sss =~ s/apple/\"$&\"/;

print $sss, "\n";

$sss = "This is an apple";

$sss =~ s/(p)(p)/$2P/;

print $sss, "\n";

$sss = "This is an apple";

$sss =~ s/apple//;

print $sss, "\n";

$sss = "This is an apple";

$sss =~ s/apple/ucfirst($&)/e;

print $sss, "\n";

$sss = "There are biscuits and candies";

$sss =~ s/\band\b/AND/g;

print $sss, "\n";

$sss = "There are biscuits and candies";

$sss =~ s/\Band\B/AND/g;

print $sss, "\n";

$sss = "ABC\nabcd\nABCDE\n";

while($sss =~ /abc/img){
  print "$& is match\n";
}

$sss = "This is an apple";

$sss =~ tr/A-Za-z/a-zA-Z/;

print $sss, "\n";

$sss = "This is an apple z";

$sss =~ tr/za-y/a-z/;

print $sss, "\n";

my @abcdef = ("abcdef", "cdef", "abcde");

my @def = grep(/def/, @abcdef);

foreach(@def){
  print $_,"\n";
}

# サブルーチン

my $x1 = 3;
my $x2 = 4;

print &add($x1, $x2), "\n";

sub add{
  my ($x, $y) = @_;

  return ($x + $y);
}

# ファイル

open(FILE, '< a.txt');
open(FILE2, '> b.txt');

while(my $line=<FILE>){
  print FILE2 $line;
  print $line;
}

close(FILE);
close(FILE2);

inserted by FC2 system