开发者

How can I limit Perl's split function?

I have trying this:

$string ="Group: ALL:ALL:Good";

@str2  = spl开发者_StackOverflowit (/:/,':',2);

print "@str2";

I am looking in $str[0] = Group and $str[1]= ALL:ALL:Good.

It not working. What would be issue?


Don't you mean this?

@str2  = split (/:/,$string,2);

Otherwise, you'll be splitting the string :, which seems kind of pointless.


my $string = "Group: ALL:ALL:Good";
my @str    = split(/:/, $string, 2);

print $str[0];
print $str[1];


To use limit with split

@array = split /PATTERN/,EXPR,LIMIT;

From PerlDoc split function:

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜