ForNext
Only Do What Only You Can Do
1.1. ファイルの COPY
VBScript
JScript
Perl
更新日 : 2008.12.26
■ 1行ずつ読んで、1行ずつ書く
use strict; print while(<STDIN>);
C:\>perl "S:\1_1_ファイルのCOPY\lesson001.pl" < "S:\1_1_ファイルのCOPY\test.txt" 1. 手はじめに 1.1. ファイルの COPY 1.2. 文字数を数える
■ 1文字ずつ読んで、1文字ずつ書く
これではだめ
use strict; my $char; print $char, "\n" while($char = getc(STDIN));
C:\>perl "S:\1_1_ファイルのCOPY\lesson002.pl" < "S:\1_1_ファイルのCOPY\test.txt" 1 . ・ ・ ・ ヘ ・ カ ・ ゚ ・ ノ 1 . 1 . ・ t ・ @ ・ C ・ ・ ・ フ C O P Y 1 . 2 . ・ カ ・ ・ ・ ・ ・ ・ ・ ・ ・ ヲ ・ ・
■ 1文字ずつ読んで、1文字ずつ書く
use strict; my $current_line = ""; my $pos = -1; my $c = ""; while (<STDIN>) { # 1行ずつ読む $current_line = $_; # 改行コードを取り除く chomp($current_line); $pos = 0; while ($pos < length($current_line)) { # 半角 / 全角 if (substr($current_line, $pos, 1) =~ /^[\x80-\xff]/) { # 2バイト取得 $c = substr($current_line, $pos, 2); $pos += 2; } else { # 1バイト取得 $c = substr($current_line, $pos, 1); $pos++; } # 1文字表示 print $c, "\n"; } }
C:\>perl "S:\1_1_ファイルのCOPY\lesson003.pl" < "S:\1_1_ファイルのCOPY\test.txt" 1 . 手 は じ め に 1 . 1 . フ ァ イ ル の C O P Y 1 . 2 . 文 字 数 を 数 え る
■ 1文字ずつ読んで、1文字ずつ書く
use strict; #****************************************************************************** # 1文字ずつ読んで、1文字ずつ書く #****************************************************************************** my $c = ""; my $EOF = "\0"; put_char($c) while (($c = get_char()) ne $EOF); #============================================================================== # 1文字 取得 #============================================================================== my $current_line = ""; my $pos = -1; my $NEWLINE = "\n"; sub get_char { my $char; # まだ読んでなかったら if ($current_line eq "") { # 1行読む $current_line = <STDIN>; # ファイルの終わりなら 終了 return $EOF if (!$current_line); # 改行コードを取り除く chomp($current_line); # 現在位置 クリア $pos = 0; } # 行の終わりに達したら if ($pos >= length($current_line)) { # 現在行 クリア $current_line = ""; # 行の終わりを 知らせる return $NEWLINE; } # 半角 / 全角 if (substr($current_line, $pos, 1) =~ /^[\x80-\xff]/) { # 2バイト取得 $char = substr($current_line, $pos, 2); $pos += 2; } else { # 1バイト取得 $char = substr($current_line, $pos, 1); $pos++; } return $char; } #============================================================================== # 1文字 出力 #============================================================================== my $buffer = ""; sub put_char { ($_) = @_; if ($_ eq $NEWLINE) { # 行の終わりなら 出力 print $buffer, "\n"; $buffer = ""; } else { # 行の終わりでなければ、バッファにためる $buffer .= $_; } }
C:\>perl "S:\1_1_ファイルのCOPY\lesson004.pl" < "S:\1_1_ファイルのCOPY\test.txt" 1. 手はじめに 1.1. ファイルの COPY 1.2. 文字数を数える
PHP
更新日 : 2008.12.26
■ 1行ずつ読んで、1行ずつ書く
<?php $fp = fopen("php://stdin", "r"); while (!feof($fp)) { print(fgets($fp)); } fclose($fp); ?>
C:\>php "S:\1_1_ファイルのCOPY\lesson001.php" < "S:\1_1_ファイルのCOPY\test.txt" 1. 手はじめに 1.1. ファイルの COPY 1.2. 文字数を数える
■ 1文字ずつ読んで、1文字ずつ書く
これではだめ
<?php $fp = fopen("php://stdin", "r"); while (!feof($fp)) { echo fgetc($fp), "\n"; } fclose($fp); ?>
C:\>php "S:\1_1_ファイルのCOPY\lesson002.php" < "S:\1_1_ファイルのCOPY\test.txt" 1 . ヘ カ ゚ ノ 1 . 1 . t @ C フ C O P Y 1 . 2 . カ ヲ
■ 1文字ずつ読んで、1文字ずつ書く
<?php $current_line = ""; $pos = -1; $c = ""; $fp = fopen("php://stdin", "r"); while (!feof($fp)) { # 1行ずつ読む $current_line = fgets($fp); # 改行コードを取り除く $current_line = rtrim($current_line, "\n\r"); $pos = 0; while ($pos < strlen($current_line)) { # 半角 / 全角 if (mb_ereg('^[\x80-\xff]', substr($current_line, $pos, 1))) { # 2バイト取得 $c = substr($current_line, $pos, 2); $pos += 2; } else { # 1バイト取得 $c = substr($current_line, $pos, 1); $pos++; } # 1文字表示 echo $c, "\n"; } } fclose($fp); ?>
C:\>php "S:\1_1_ファイルのCOPY\lesson003.php" < "S:\1_1_ファイルのCOPY\test.txt" 1 . 手 は じ め に 1 . 1 . フ ァ イ ル の C O P Y 1 . 2 . 文 字 数 を 数 え る
■ 1文字ずつ読んで、1文字ずつ書く
<?php #****************************************************************************** # 1文字ずつ読んで、1文字ずつ書く #****************************************************************************** $c = ""; $EOF = "\0"; $current_line = ""; $pos = -1; $NEWLINE = "\n"; $buffer = ""; while (($c = get_char()) != $EOF) { put_char($c); } #============================================================================== # 1文字 取得 #============================================================================== function get_char() { global $fp; global $EOF; global $current_line; global $pos; global $NEWLINE; $char = ""; # まだ読んでなかったら if ($current_line == "") { # まだ OPEN してなかったら if ($pos == -1) { $fp = fopen("php://stdin", "r"); } # 1行読む $current_line = fgets($fp); # ファイルの終わりなら 終了 if (feof($fp)) { fclose($fp); return $EOF; } # 改行コードを取り除く $current_line = rtrim($current_line, "\n\r"); # 現在位置 クリア $pos = 0; } # 行の終わりに達したら if ($pos >= strlen($current_line)) { # 現在行 クリア $current_line = ""; # 行の終わりを 知らせる return $NEWLINE; } # 半角 / 全角 if (mb_ereg('^[\x80-\xff]', substr($current_line, $pos, 1))) { # 2バイト取得 $char = substr($current_line, $pos, 2); $pos += 2; } else { # 1バイト取得 $char = substr($current_line, $pos, 1); $pos++; } return $char; } #============================================================================== # 1文字 出力 #============================================================================== function put_char($char) { global $NEWLINE; global $buffer; if ($char == $NEWLINE) { # 行の終わりなら 出力 echo $buffer, "\n"; $buffer = ""; } else { # 行の終わりでなければ、バッファにためる $buffer .= $char; } } ?>
C:\>php "S:\1_1_ファイルのCOPY\lesson004.php" < "S:\1_1_ファイルのCOPY\test.txt" 1. 手はじめに 1.1. ファイルの COPY 1.2. 文字数を数える
Python
Ruby
更新日 : 2008.12.26
■ 1行ずつ読んで、1行ずつ書く
STDIN.each do |line| puts line end
C:\>ruby "S:\1_1_ファイルのCOPY\lesson001.rb" < "S:\1_1_ファイルのCOPY\test.txt" 1. 手はじめに 1.1. ファイルの COPY 1.2. 文字数を数える
■ 1文字ずつ読んで、1文字ずつ書く
STDIN.each do |line| line.split(//s).each do |char| puts char end end
C:\>ruby "S:\1_1_ファイルのCOPY\lesson002.rb" < "S:\1_1_ファイルのCOPY\test.txt" 1 . 手 は じ め に 1 . 1 . フ ァ イ ル の C O P Y 1 . 2 . 文 字 数 を 数 え る
■ 1文字ずつ読んで、1文字ずつ書く
#****************************************************************************** # 1文字ずつ読んで、1文字ずつ書く #****************************************************************************** #============================================================================== # 1文字 取得 #============================================================================== $current_line = [] $pos = -1 $NEWLINE = "\n" $EOF = "\0"; def get_char # まだ読んでなかったら if $current_line.size == 0 # 1行読む line = $stdin.gets # ファイルの終わりなら 終了 return $EOF unless line # 改行コードを取り除く line.chomp!; # 文字の配列に分解 $current_line = line.split(//s) # 現在位置 クリア $pos = 0; end # 行の終わりに達したら if $pos >= $current_line.size # 現在行 クリア $current_line = [] # 行の終わりを 知らせる return $NEWLINE; end char = $current_line[$pos] $pos += 1 return char end #============================================================================== # 1文字 出力 #============================================================================== $buffer = "" def put_char(char) if char == $NEWLINE # 行の終わりなら 出力 puts $buffer $buffer = "" else # 行の終わりでなければ、バッファにためる $buffer << char end end #============================================================================== # 1文字ずつ読んで、1文字ずつ書く #============================================================================== c = "" put_char c while (c = get_char) != $EOF
C:\>ruby "S:\1_1_ファイルのCOPY\lesson004.rb" < "S:\1_1_ファイルのCOPY\test.txt" 1. 手はじめに 1.1. ファイルの COPY 1.2. 文字数を数える