perl 文字列処理関数 split 2

#split関数を使ってスペース文字の前後の文字列を取り出す

print "文字列を入力してください>";
$string = <STDIN>;
chomp($string);

#スペース文字で2つに区切る
($pre, $post) = split(/\s+/, $string, 2);

#スペース文字の前後の文字列を表示
print "最初の文字列:$pre\n";
print "後の文字列:$post\n";

#実行結果
# 文字列を入力してください>perl is a Practical Extraction and Report Language
# 最初の文字列:perl
# 後の文字列:is a Practical Extraction and Report Language

初めてのPerl 第6版

初めてのPerl 第6版