++Diary++

2003年10月の日記

2003年10月24日(金)
お久しぶりです
2003年10月27日(月)
Perl覚え書き
2003年10月30日(木)
Perl覚え書き
お久しぶりです
久しぶりに自分で日記を覗いて見たら、何と前回の書き込みは8月・・
あれから何だかんだと忙しくて日記を書く暇がなかった。
それにしても今までの日記を読み返して見ると、毎年夏になるとProgramming Perlを読み返しているなあと思った。今見たら二つ前の「Perl覚え書き」なる記事の内容が一瞬意味不明で、しばらく眺めていたらやっと何が書いてあるか分かり、こんなに記憶力が衰えているのはやっぱり年のせいかと愕然とした。
(あの記事に書いてあることは結構役立ちます。)
ということでまた久しぶりにProgramming Perlを読んでいる。

Programming Perlだけでなく最近の私は一度読んだ本の内容をしばらく経つと忘れるという特技(!?)があるので、推理小説など何度でも楽しめて経済的かもである。またテレビ番組も同様で、サスペンスものなど好きなのだが、一度見た番組を再放送で見ても再放送とは思わずに楽しめる。(もしかしてこれがボケの始まりなのだろうか。)

テレビと言えば昨日の午後ふとテレビのスイッチを入れて見たらワイドショーで、皇室の人間になりすまして祝儀を騙し取ったという詐欺の罪で逮捕された女性のことを放送していた。何でもその女性はとても弁が立って、本当でもないことを本当のようにしゃべっていたそうである。
でもちょっと考えるとこういう人って詐欺罪になる程でないにしても結構いるんじゃないかと思う。たとえばネット上でも、他の人が書いたPerlのコードの変数名だけ変えたものを「オリジナルスクリプトです」といっている人もいるという話だし、素材に関して言えば人が作った画像をさも自分が作ったかのように自分のサイトで配付している人もいるそうだ。
まあ、ネット上では顔が見えない分だけさらにそういうことは多いのかもしれない。

ところで個人的に最近の出来事で嬉しかったことは、前に製造中止になったセデスG(頭痛薬)と同じ薬がまた製造されるようになったことだろうか。
(名前こそ「SG」などという変なものになっているが中身は同じだ。)
幸せなことに私はこの薬を箱で手に入れたので、頭が痛くなる度にイソイソとクスリを飲んでいる。
やっぱり私ってかなりおかしいのかも。
2003年10月24日(金)  No.44

shiromuku  2003/12/01/23:36:02   No.49
現在感想のテスト書き込みは出来ません。

Perl覚え書き
■the m// Operator (Matching)

1. In scalar context, the operator returns true (1) if successful, false ("") otherwise.

ex.) If ($shire =~ /Baggins/) {..........}

2. In list context, m// returns a list of substrings matched by the capturing parentheses in the pattern.
In list context, m//g returns a list of all matches found.
If there are no capturing parentheses within the /g pattern, then the complete matches are returned. If there are capturing parentheses, then only the strings captured are returned.
If the match fails in list context, a null list is returned.
If the the match succeeds in list context, but there were no capturing parentheses nor /g, a list value (1) is returned.

ex.) If (($key, $value) = $string =~ /(\w+): (.*)/) {..........}
ex.) If (@perls = $paragraph =~ /perl/gi) {..........}

3. その他

ex.) %hash = $string =~ /(\w+)=(\w+)/g;

■the s/// Operator (Substitution)

1. The return value of an s/// operation (in scalar and list contexts alike) is the number of times it succeeded (which can be more than once if used with the /g modifier).
On failure, since it substituted zero times, it returns false(""), which is numerically equivalent to 0.

ex.) If ($lotr =~ s/Bilbo/Frodo/) {..........}
ex.) $change_count = $lotr =~ s/Bilbo/Frodo/g;

2. その他

ex.) s/revision|version|release/\u$&/g; #大文字化
ex.) s{version\s+([0-9.]+)}{$Names{$1}?"The $Names{$1} release":$&}xge; #コードの実行結果が置換文字列として使われる
ex.) ($date = $pp_data->[4]) =~ s/^\d{4}\///g; #shiromuku(r2)BBS
ex.) for (@new = @old) {s/Bilbo/Frodo/} #@oldの内容を変えたくない場合
ex.) for ($new = $old) {s/^\s+//; s/\s+$//; s/\s+/ /;} #$newの先頭の空白文字を削除、最後の空白文字を削除、中間にある空白文字の連続を一個の空白文字に変える
ex.) 1 while $number =~ s/(\d)(\d\d\d)(?!\d)/$1,$2/; #数字にコンマ付加
ex.) 1 while $string =~ s/\b(\w+) \1\b/$1/gi; #"this this"→"this"

■Capturing and Clustering

ex.) /foo((?-i)bar)/i #"bar" must be lower case(Capturingあり)
   /foo(?-i:bar)/i #"bar" must be lower case(Capturingなし)

■Alternation

ex.) /pro(b|n|r|l)ate/ #(Capturingあり)
   /pro(?:b|n|r|l)ate/ #(Capturingなし)

ex.) /com(pound|)/ #Matches "compound" or "com"("com"の場合$1はdefined null string)
   /com(pound)?/ #Matches "compound" or "com"("com"の場合$1はundefined)

■qr/pattern/imosx

 ex.) $cat = qr/\d+/;
if ($filename =~ /^($cat)\_(\w+)\.cgi$/o) {..........}

ex.) @regexes = ();
@regexes = map {qr/$_/} @patterns;
foreach $item(@data) {
foreach $re(@regexes) {
if ($item =~ /$re/) {..........}
}
}


■Lookaround Assertions
ex.) 1 while $string =~ s/\b(\w+)\s(?=\1\b)//gi; #positive lookahead("this this"→"this")
ex.) 1 while $string =~ s/\b(\w+)\s(?=\1\b(?!'\w))//gi; #negative lookahead("The clothes you DON DON't fit."を回避)
ex.) s/(?<=c)ei/ie/g #positive lookbehind (前に"c"がある"ei"を"ie"に置換
ex.) s/(?<!c)ei/ie/g #negative lookbehind (前に"c"がない"ei"を"ie"に置換

■the /e modifier

ex.) s/(\d+)/$1*2/e; #"42"を"84" に置換
ex.) s/\b(\d+\.?\d*)C\b/int($1*1.8+32) . "F"/e; #233C → 451F
ex.) s/^(\d+)(?=:)/$1+100/e; #行の最初の数字(後に":"が付いている)を100増加
ex.) s/(\$\w+)/$1/eg; #スカラー変数を展開
ex.) $_ = "I have 4+19 dollars and 8/2 cents.\n";
s{(\d+\s*[+*/-]\s*\d+)}{$1}eg;
print; #"I have 23 dollars and 4 cents."になる
2003年10月27日(月)  No.45

shiromuku  2004/01/12/10:08:46   No.56
現在感想のテスト書き込みは出来ません。

Perl覚え書き
■Creating References

1. You can create areference to any named variable or subroutine with a backslash.
  $scalarref = \$foo;
  $arrayref = \@ARGV;
  $hashref = \%ENV;
  $coderef = \&handler;
  $globref = \*STDOUT;

2. You can create areference to an anonymous array with square brackets.
  $arrayref = [1,2,['a','b','c','d']];

3. You can create areference to an anonymous hash with braces.
  $hashref = {'Adam' => 'Eve','Clyde' => $bonnie,'Antony' => 'Cleo' . 'patra',};

4. You can create areference to an anonymous subroutine by using sub without a subroutine name.
  $coderef = sub{print "Boink!\n"}; #&$$coderef prints "Boink!"

5. References to filehandles or directory handles can be created by referring the typeglob of the same name.
  &splutter(\*STDOUT);
  Sub splutter {
   my $fh = shift;
   print $fh "..........\n";
  }

6. References can be created by dereferencing them.

■Dereferencing References

1. Using a Variable as a Variable Name
  $bar = $$scalarref;

  push (@$arrayref, $filename);
  $$arrayref[0] = "January";
  @$arrayref[4..6] = qw/May June July/;

  %$hashref = (KEY => "RING", BIRD => "SING");
  $$hashref{KEY} = "VALUE";
  @$hashref{"KEY1","KEY2"} = ("VAL1", "VAL2");

  &$coderef(1,2,3);

2. Using a BLOCK as a Variable Name
  $bar = ${$scalarref};

  push (@{$arrayref}, $filename);
  ${$arrayref}[0] = "January";
  @{$arrayref}[4..6] = qw/May June July/;

  %{$hashref} = (KEY => "RING", BIRD => "SING");
  ${$hashref}{KEY} = "VALUE";
  @{$hashref}{"KEY1","KEY2"} = ("VAL1", "VAL2");

  &{$coderef}(1,2,3);

3. Using the Arrow Operator
  $$arrayref[0] = "January";
  ${$arrayref}[0] = "January";
  $arrayref->[0] = "January";
  (上記のどの書き方でもOK)

  &$coderef(1,2,3);
  &{$coderef}(1,2,3);
  $coderef->(1,2,3);
  (上記のどの書き方でもOK)

  $listref->[2][2] = "hello";
  $$listref[2][2] = "hello";
  (上記のどの書き方でもOK)

■Array of Arrays
※以下はすべて同じ

for $i (1..10) {
 @array = somefunc ($i);
 $AoA[$i] = [@array];
}

for $i (1..10) {
 @array = somefunc ($i);
 @{$AoA[$i]} = @array;
}

for $i (1..10) {
 my @array = somefunc ($i);
 $AoA[$i] = \@array;
}

for $i (1..10) {
 $AoA[$i] = [somefunc ($i)];
}

■Hash of Arrays

for $group ("simpsons", "jetsons","flintstones") {
 $HoA{$group} = [get_famly($group)];
}

$HoA{$group}[1] =~ s/(\w)/\u$1/; #最初の文字を大文字に

for $family (sort {@{$HaA{$b}} <=> @{$HaA{$a}}} keys %HoA) {
 print "$family: ", join(",", sort @{$HaA{$family}}), "\n";
} #各Arrayの要素数で、Hashのキーを多い方からソートして、キーと要素(アスキー順にソートしたもの)を順にプリント

■Hash of Hashes

for $family (sort {keys %{$HaH{$b}} <=> keys %{$HaH{$a}}} keys %HoH) {
 print "$family: ";
 for $role (sort keys %{$HaH{$family}}) {
  print "$role=$HaH{$family}{$role}";
 }
 print "\n";
} #各Hashの要素数(キーの数)で、Hashのキーを多い方からソートして、キーと要素(アスキー順にソートしたもの)を順にプリント
2003年10月30日(木)  No.47

shiromuku  2003/11/30/16:02:50   No.48
現在感想のテスト書き込みは出来ません。

No. PASS
 OR AND
スペースで区切って複数指定可能
++HOME++
[日記一覧] [最新]
shiromuku(fs2)DIARY version 1.40