Learning Perl: 9.3. The join Function
来源:互联网 发布:添加打印机无usb端口 编辑:程序博客网 时间:2024/05/22 05:15
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, 6, 8, 10, 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.
- Learning Perl: 9.3. The join Function
- Learning Perl: 2.11. The defined Function
- Perl Learning: 2.6. The if Control Structure
- Learning Perl: 2.8. The chomp Operator
- Perl Learning: 2.9. The while Control Structure
- Perl Learning: 2.10. The undef Value
- Learning Perl: 3.6. The foreach Control Structure
- Learning Perl: 4.8. The use strict Pragma
- Learning Perl: 4.9. The return Operator
- Learning Perl: 5.3. The Invocation Arguments
- Learning Perl: 8.4. The Binding Operator, =~
- Learning Perl: 8.6. The Match Variables
- Learning Perl: 9.2. The split Operator
- Learning Perl: 10.1. The unless Control Structure
- Learning Perl: 10.2. The until Control Structure
- Learning Perl: 10.5. The elsif Clause
- perl join
- Learning Perl
- C#字符串处理函数整理之substring
- C/C++中关于地址、指针和引用变量的学习笔记(十) : 引用型变量
- Learning Perl: 9.2. The split Operator
- PHP中双引号和单引号的区别和用法
- 10.10 信号_alarm和pause函数
- Learning Perl: 9.3. The join Function
- Learning Perl: 9.4. m// in List Context
- 瑞斯康达NView 5 研究实录 (6) - 功能实现二
- java Script查询列表排序
- Learning Perl: 9.5. More Powerful Regular Expressions
- Learning Perl: 10.1. The unless Control Structure
- 重置cdr的billsec的方法
- 飞秋_飞秋2010_飞秋2010下载_飞秋下载2010正式版
- Learning Perl: 10.2. The until Control Structure