星期二, 一月 22, 2008

フィルターとしての Perl

フィルターとしての Perl


テキストの改行コード

perl tips

from Perl Online Document

split /PATTERN/,EXPR,LIMIT
split /PATTERN/,EXPR
split /PATTERN/
split

Splits the string EXPR into a list of strings and returns that list. By default, empty leading fields are preserved, and empty trailing ones are deleted. (If all fields are empty, they are considered to be trailing.)

If LIMIT is specified and positive, it represents the maximum number of fields the EXPR will be split into, though the actual number of fields returned depends on the number of times PATTERN matches within EXPR. If LIMIT is unspecified or zero, trailing null fields are stripped (which potential users of pop would do well to remember). If LIMIT is negative, it is treated as if an arbitrarily large LIMIT had been specified. Note that splitting an EXPR that evaluates to the empty string always returns the empty list, regardless of the LIMIT specified.


my $hoge = 'a,b,c,,';
my @foo = split /,/, $hoge; # @foo = ('a', 'b', 'c')
my @bar = split /,/, $hoge, -1; # @bar = ('a', 'b', 'c', '', '')