LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1232|回复: 5

split的疑惑~~~~

[复制链接]
发表于 2004-4-13 00:58:48 | 显示全部楼层 |阅读模式
我现在学perl用的是一套电子教程,写得不是很深入,有些地方写得很朦胧,看得我一头雾水,介绍split用法也是一样,我想烦请哪位DX举个例子给我说一下split的用法 :help
发表于 2004-4-13 02:16:43 | 显示全部楼层
是splice吧,用于拼接数组的 ?
 楼主| 发表于 2004-4-13 02:25:51 | 显示全部楼层
split是用于分割字符串的
发表于 2004-4-13 04:11:49 | 显示全部楼层
举俩个例子:
1这个例子检测键入的$_,如果输入4个字母的单词则显示Yes,否则显示No
  1. /home/javalee#perl -
  2. while(<>){for(split){if(/^\w{4}$/){print "Yes!!!\n"}else{print "error!!!\n"}}};__END__
  3. linux
  4. error!!!
  5. shell
  6. error!!!
  7. perl
  8. Yes!!!
  9. exit
  10. Yes!!!
  11. quit
  12. Yes!!!
  13. Quit
复制代码

2这个例子将split返回的表中提取多个单词
  1. /home/javalee#perl -
  2. $,=" ";
  3. print ((split ' ','I Love Linux and Perl!')[0,4],"\n");
  4. __END__
  5. I Perl!
复制代码
 楼主| 发表于 2004-4-13 10:14:51 | 显示全部楼层
谢谢~~
发表于 2004-4-14 16:28:07 | 显示全部楼层
这里有split 的解释。在置顶也有些例子。。。

  1. split ([/PATTERN/], [EXPR], [LIMIT])

  2. Category: Array, Regular Expression
  3. Return Value in Scalar Context: Not recommended, but it returns the number of fields found and stored the fields in the @_ array.
  4. Return Value in Array Context: A list of fields found in EXPR or $_ if no expression is specified.
  5. Definition: Splits a string expression into fields based on the delimiter specified by PATTERN. If no pattern is specified whitespace is the default. An optional limit restricts the number of elements returned. A negative limit is the same effect as no limit. This fuNCtion is often used in conjuNCtion with join() to create small text databases.

  6. @fields = split(/:/, "1:2:3:4:5");


复制代码


  1. #!/usr/bin/perl -w
  2. #@list=split(parrern,sting,maxlength);
  3. #
  4. @text=("well","hello,the world","how","do","you","do");
  5. print "@text\n";
  6. @name=split(/,/,@text,2);
  7. print "@name\n";
  8. print "@text\n";
  9. $abc = "apile:fjkdfk:300:500:XXX:/bin/bash";

  10. @abc = split(/:/,$abc);
  11. print "@abc\n";
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表