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

Learning Perl: 9.3. The join Function

发布时间:2020-12-15 20:55:45 所属栏目:大数据 来源:网络整理
导读:? 9.3. The join Function The join function doesn't use patterns but performs the opposite function of split : split breaks up a string into a number of pieces,and join glues together a bunch of pieces to make a single string. The join func

Previous Page

Next Page

?

9.3. The join Function

The join function doesn't use patterns but performs the opposite function of split: split breaks up a string into a number of pieces,and join glues together a bunch of pieces to make a single string. The join function looks like this:

    my $result = join $glue,@pieces;

The first argument to join is the glue,which may be any string. The remaining arguments are a list of pieces. join puts the glue string between the pieces and returns the resulting string:

    my $x = join ":",4,6,8,10,12;  # $x is "4:6:8:10:12"

In that example,we had five items,so there are only four colons or four pieces of glue. The glue shows up only between the pieces,never before or after them. So,there will be one fewer piece of glue than the number of items in the list.

This means there may be no glue at all if the list doesn't have at least two elements:

    my $y = join "foo","bar";       # gives just "bar",since no fooglue is needed
    my @empty;                       # empty array
    my $empty = join "baz",@empty;  # no items,so it's an empty string

Using $x from above,we can break up a string and put it back together with a different delimiter:

    my @values = split /:/,$x;  # @values is (4,12)
    my $z = join "-",@values;   # $z is "4-6-8-10-12"

Though split and join work well together,don't forget that the first argument to join is always a string,not a pattern.

Previous Page

Next Page

(编辑:李大同)

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

    推荐文章
      热点阅读