split, which breaks up a string ac-cording to a pattern.
@fields = split /separator/, $string;
The default for split is to break up $_ on whitespace:
my @fields = split; # like split /\s+/, $_;
@fields = split /:/, "abc:def:g:h"; # gives ("abc", "def", "g", "h")
You could even have an empty field, if there were two delimiters together:
@fields = split /:/, "abc:def::g:h"; # gives ("abc", "def", "", "g", "h")
Here’s a rule that seems odd at first, but it rarely causes problems: leading empty fields
are always returned, but trailing empty fields are discarded. For example:‖
@fields = split /:/, ":::a:b:c:::"; # gives ("", "", "", "a", "b", "c")
A "split" on "/\s+/" is like a "split(' ')" except that any leading
whitespace produces a null first field. A "split" with no
arguments really does a "split(' ', $_)" internally.
- $str = " 10 30 st mmmmm ";
-
-
@f = split /\s+/, $str;
-
print "@f\n";
-
-
@s = split(' ', $str);
-
@d = split(' ', $str);
-
print "@s\n";
-
print "@d\n";
-
-
# 10 30 st mmmmm
-
#10 30 st mmmmm
-
#10 30 st mmmmm
join glues together a bunch of pieces to make a single string. The join function looks like this:
my $result = join $glue, @pieces;
阅读(447) | 评论(0) | 转发(0) |