让我们看看一个名为split()的Perl函数,它具有以下语法-
split [ PATTERN [ , EXPR [ , LIMIT ] ] ]
此函数将字符串拆分为字符串数组并返回它。如果指定了LIMIT,则最多拆分为该数量的字段。如果省略PATTERN,则在空白处分割。以下是示例-
#!/usr/bin/perl # define Strings $var_string = "Rain-Drops-On-Roses-And-Whiskers-On-Kittens"; $var_names = "Larry,David,Roger,Ken,Michael,Tom"; # transform above strings into arrays. @string = split('-', $var_string); @names = split(',', $var_names); print "$string[3]\n"; # This will print Roses print "$names[4]\n"; # This will print Michael
输出结果
这将产生以下结果-
Roses Michael