PHP

ファイルの COPY

PHP

1.1. ファイルの COPY 1行ずつ読んで、1行ずつ書く lesson001.php 実行結果 C:\>php "C:\study\SoftwareTools\1_1_ファイルのCOPY\lesson001.php" ftwareTools\1_1_ファイルのCOPY\test.txt" 1. 手はじめに 1.1. ファイルの COPY 1.2. 文字数を数える 1…

comparison operators

comparison operators Perl $a = 1; $b = 1; if ($a == $b) { print "$a == $b\n" } $b = 2; if ($a != $b) { print "$a != $b\n" } $a = 2; $b = 1; if ($a > $b) { print "$a > $b\n" } $a = 1; $b = 1; if ($a >= $b) { print "$a >= $b\n" } $a = 1; $b …

logical operators

logical operators Perl sub return_true { print "return_true"; return 1; } sub return_false { print "return_false"; return 0; } print "&& "; if (return_false() && return_true()) { } print "\n"; print "and "; if (return_false() and return_tr…

function declaration

function declaration Perl sub add { $_[0] + $_[1] } print add(1, 2); # 3 Ruby def add(a, b) a + b end print add(1, 2) # 3 PHP Python def add(a, b): return a + b print add(1, 2) # 3

break, continue

break, continue Perl $n = 0; foreach $i (0..10) { print "$i, $n\n"; $n++; next if ($n % 2 == 0); redo if ($n % 3 == 0); last if ($n > 5); print "\n"; } print "$n\n"; L:\>perl lesson010.pl 0, 0 1, 1 2, 2 2, 3 3, 4 4, 5 5, 6 7 Ruby n = 0 for…

for, foreach

for, foreach Perl for ( $i=0; $i <= 10; $i++ ) { print "$i\n" # 0 - 10 } print "\n"; for $i (0..10) { print "$i\n" # 0 - 10 } print "\n"; foreach $i (0..10) { print "$i\n" # 0 - 10 } Ruby for i in 0..10 puts i # 0 - 10 end puts (0..10).eac…

while

while Perl $n = 0; while ($n < 10) { print $n++, "\n"; # 0 - 9 } print $n, "\n\n"; # 10 $n = 0; print $n++, "\n" while ($n < 10); # 0 - 9 print $n, "\n\n"; # 10 $n = 0; until ($n >= 10) { print $n++, "\n" # 0 - 9 } print $n, "\n\n"; # 10 $…

do 〜 while

do 〜 while Perl $n = 0; do { print $n++, "\n" # 0 - 9 } while ($n < 10); print $n, "\n\n"; # 10 $n = 0; print $n++, "\n" while ($n < 10); # 0 - 9 print $n, "\n\n"; # 10 $n = 0; do { print $n++, "\n" # 0 - 9 } until ($n >= 10); print $n, "…

if, else

if, else Perl $n = 2; if ( 0 == $n ) { print "no hits\n" } elsif ( 1 == $n ) { print "1 hit\n" } else { print "$n hits\n" # 2 hits } unless ( 0 != $n ) { print "no hits\n" } else { print "$n hits\n" # 2 hits } print "$n hits\n" if ($n > 1)…

operators

operators Perl $a = 5; $b = 2; printf("a + b = %d\n", $a + $b ); # a + b = 7 printf("a - b = %d\n", $a - $b ); # a - b = 3 printf("a * b = %d\n", $a * $b ); # a * b = 10 printf("a ** b = %d\n", $a ** $b ); # a ** b = 25 printf("a / b = %f\…

swap

swap Perl $a = 1; $b = 2; print "a = ", $a, "\n"; # a = 1 print "b = ", $b, "\n"; # b = 2 print "\n"; ($a, $b) = ($b, $a); print "a = ", $a, "\n"; # a = 2 print "b = ", $b, "\n"; # b = 1 Ruby a = 1 b = 2 print "a = ", a, "\n" # a = 1 print…

parallel assignment

parallel assignment Perl ($a, $b, $c) = (1, 2, 3); print "a = ", $a, "\n"; # a = 1 print "b = ", $b, "\n"; # b = 2 print "c = ", $c, "\n"; # c = 3 print "\n"; # 6 is ignored: ($a, $b) = (4, 5, 6); print "a = ", $a, "\n"; # a = 4 print "b =…

assignment

assignment Perl $a = 1; print "a = ", $a, "\n"; # a = 1 $a = $b = 2; print "a = ", $a, "\n"; # a = 2 print "b = ", $b, "\n"; # b = 2 Ruby a = 1 print "a = ", a, "\n" # a = 1 a = b = 2 print "a = ", a, "\n" # a = 2 print "b = ", b, "\n" # b…

Hello, World!

Hello, World! Perl print 'Hello, World!\n'; # Hello, World!\n print "\n"; print "Hello, World!\n"; # Hello, World! $var1 = "Hello"; $var2 = "World"; print $var1, ", ", $var2, "!", "\n"; # Hello, World! print "$var1, $var2!\n"; # Hello, Wor…

reflection and hooks

スクリプト言語の比較 http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。 reflection and hooks perl php python ruby class ref $a type(a) a.class has method? $a->can('reverse') hasattr(a,'reverse') a.respond…

objects

スクリプト言語の比較 http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。 objects perl php python ruby define class package Int; use Moose; has 'value' => ( is => 'rw' ); around BUILDARGS => sub { my $orig = …

libraries and modules

スクリプト言語の比較 http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。 libraries and modules perl php python ruby load library require 'Foo.pm'; # or require Foo; # or use Foo; import foo require 'foo' # …

environment and i/o

スクリプト言語の比較 http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。 environment and i/o perl php python ruby external command system('ls'); import os os.system('ls') system('ls') backticks `ls`; impor…

execution control

スクリプト言語の比較 http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。 execution control perl php python ruby if if ( 0 == $n ) { print "no hits\n" } elsif ( 1 == $n ) { print "1 hit\n" } else { print "$n …

functions

スクリプト言語の比較 http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。 functions perl php python ruby function declaration sub add { $_[0] + $_[1] } def add(a,b): return a+b def add(a,b); a+b; end functio…

containers

スクリプト言語の比較 http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。 containers perl php python ruby array literal @nums = (1,2,3,4); nums = [1,2,3,4] nums = [1,2,3,4] array size $#nums + 1 or scalar(@n…

strings

スクリプト言語の比較 http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。 strings perl php python ruby character literal none none none none chr and ord chr(65) ord("A") chr(65) ord('A') 65.chr "A".ord strin…

arithmetic and logic

スクリプト言語の比較 http://hyperpolyglot.org/scripting を参考に、スクリプト言語の勉強をしてまいります。 arithmetic and logic perl php python ruby true and false 1 0 True False true false falsehoods undef 0 0.0 '' '0' () False None 0 0.0…

スクリプト言語の比較

スクリプト言語の比較 http://hyperpolyglot.org/scripting よりshow versionPHP$ php --versionPerl$ perl --versionPython$ python -VRuby$ ruby --version interpreterPHP$ php -f foo.phpPerl$ perl foo.plPython$ python foo.pyRuby$ ruby foo.rb replP…

ファイル操作

PHP

指定フォルダ以下のファイルを 年月別のフォルダにコピーする このような画像フォルダがあって... C:\>php c:\study\php\chapter005\lesson004.php c:\study\picture 2008/01/01 00:00:00 c:\study\picture\100CDPFP\IMGA0001.JPG 2008/01/02 00:00:00 c:\st…

ファイル検索

PHP

指定フォルダ以下のファイルを列挙する (更新時刻順にソート) lesson005.php $value) { print $value." ".$key."\n"; } function putsDir($dir, &$h) { if (is_dir($dir)) { $dh = opendir($dir); while ($file = readdir($dh)) { if ($file == ".") continu…

ファイル検索

PHP

指定フォルダ以下のファイルを列挙する (更新時刻も表示) lesson004.php

ファイル検索

PHP

指定フォルダ以下のフォルダ/ファイルを列挙する lesson001.php 実行結果 C:\>php c:\study\php\chapter005\lesson001.php c:\study\php . .. chapter001 chapter002 chapter003 chapter004 chapter005 ちょいと変更 lesson002.php

文字数 と バイト数

PHP

文字数 と バイト数 lesson011.php ", mb_strlen($value, "SJIS"), "\n"; } echo "バイト数\n"; foreach( $argv as $value ) { echo $value, " => ", strlen($value), "\n"; } ?>実行結果 C:\>php c:\study\php\chapter001\lesson011.php 12 12 12 文字数…

PHP で Excel

↓ こんな Excel ファイルを読む A B C D E F G H I J 1 A1 B1 C1 D1 E1 F1 G1 H1 J1 2 A2 B2 C2 D2 E2 F2 G2 H2 J2 3 A3 B3 C3 D3 E3 F3 G3 H3 J3 4 A4 B4 C4 D4 E4 F4 G4 H4 J4 5 A5 B5 C5 D5 E5 F5 G5 H5 J5 6 J6 7 A7 B7 C7 D7 E7 F7 G7 H7 I7 J7 Excel …