加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Learning Perl: 9.2. The split Operator

发布时间:2020-12-15 20:55:43 所属栏目:大数据 来源:网络整理
导读:? 9.2. The split Operator Another operator that uses regular expressions is split ,which breaks up a string according to a pattern. This is useful for tab-separated,colon-separated,whitespace-separated,or anything -separated data. [ ] Anyw

Previous Page

Next Page

?

9.2. The split Operator

Another operator that uses regular expressions is split,which breaks up a string according to a pattern. This is useful for tab-separated,colon-separated,whitespace-separated,or anything-separated data.[

] Anywhere you can specify the separator with a regular expression (generally,it's a simple regular expression),you can use split. It looks like this:

[

] Except "comma-separated values," normally called CSV files. Those are a pain to do with split; you're better off getting the Text::CSV module from CPAN.

    @fields = split /separator/,$string;

The split operator[

] drags the pattern through a string and returns a list of fields (substrings) that were separated by the separators. Whenever the pattern matches,that's the end of one field and the start of the next. So,anything that matches the pattern will never show up in the returned fields. Here's a typical split pattern,splitting on colons:

[

] It's an operator,though it acts a lot like a function,and everyone generally calls it a function. But the technical details of the difference are beyond the scope of this book.

    @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","","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:[*]

[*] This is merely the default. It's this way for efficiency. If you worry about losing trailing empty fields,use -1 as a third argument to split and they'll be kept. See the perlfunc manpage.

    @fields = split /:/,":::a:b:c:::";  # gives ("","a","b","c")

It's common to split on whitespace using //s+/ as the pattern. Under that pattern,all whitespace runs are equivalent to a single space:

    my $some_input = "This  is a /t        test./n";
    my @args = split //s+/,$some_input;  # ("This","is","test.")

The default for split is to break up $_ on whitespace:

    my @fields = split;  # like split //s+/,$_;

This is almost the same as using //s+/ as the pattern except,in this special case,a leading empty field is suppressed. So,if the line starts with whitespace,you won't see an empty field at the start of the list. (If you'd like to get the same behavior when splitting another string on whitespace,use a single space in place of the pattern: split ' ',$other_string. Using a space instead of the pattern is a special kind of split.)

Generally,the patterns used for split are as simple as the ones you see here. But if the pattern becomes more complex,avoid using memory parentheses in the pattern. See the perlfunc manpage for more information.[

]

[

] You might want to check out the nonmemory grouping-only parentheses notation as well in the perlre manpage.

Previous Page

Next Page

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读