Chinaunix

标题: PERL两个字符串,不关心大小写 [打印本页]

作者: slwj    时间: 2014-04-01 14:52
标题: PERL两个字符串,不关心大小写
大家好,请教下,如何使用PERL来比较两个字符串,而不关心它的大小写,多谢!
作者: pitonas    时间: 2014-04-01 15:19
  1. while(<DATA>){
  2.   my ( $one, $two) = split;
  3.   say lc($one) eq lc($two) ? 'yes' : 'no';
  4. }
  5. __DATA__
  6. abc Abc
  7. abc abd
  8. abc ABC
复制代码

作者: huang6894    时间: 2014-04-01 15:21
回复 2# pitonas


    niubility~
作者: slwj    时间: 2014-04-01 15:34
pitonas 发表于 2014-04-01 15:19


牛,不过想问下,
my ( $one, $two) = split;
,这里的split后面怎么是空的?
作者: pitonas    时间: 2014-04-01 15:38
split后面是空的 == split 空格

回复 4# slwj


   
作者: 黑色阳光_cu    时间: 2014-04-01 15:44
  1. say "\L$one\E" eq "\L$two\E" ? 'yes' : 'no';
复制代码

作者: slwj    时间: 2014-04-01 15:50
pitonas 发表于 2014-04-01 15:38
split后面是空的 == split 空格

回复 4# slwj


那split是分割哪个字符串呢,$_?
作者: yestreenstars    时间: 2014-04-01 15:55
回复 7# slwj

split后面不接参数表示以空格分割$_
   
作者: q1208c    时间: 2014-04-02 08:27
我是真心不建议使用 $_ 这个变量.
作者: jason680    时间: 2014-04-02 09:11
回复 4# slwj

study more detail ...

$ perldoc -f split
    split /PATTERN/,EXPR,LIMIT
    split /PATTERN/,EXPR
    split /PATTERN/
    split   Splits the string EXPR into a list of strings and returns that
            list. By default, empty leading fields are preserved, and empty
            trailing ones are deleted. (If all fields are empty, they are
            considered to be trailing.)
    ...



作者: 104359176    时间: 2014-04-02 21:00
Perl新的版本可能要废除 $_ ,代码不但是用来运行的,还是给未来的自己看的。
作者: qianyemlf    时间: 2014-04-03 12:24
lc($one) eq lc($two)
lc什么意思?
作者: jason680    时间: 2014-04-03 13:33
本帖最后由 jason680 于 2014-04-03 13:34 编辑

回复 12# qianyemlf

$ perldoc -f lc
    lc EXPR
    lc      Returns a lowercased version of EXPR. This is the internal
            function implementing the "\L" escape in double-quoted strings.

            If EXPR is omitted, uses $_.


$ perldoc -f uc
    uc EXPR
    uc      Returns an uppercased version of EXPR. This is the internal
            function implementing the "\U" escape in double-quoted strings. It
            does not attempt to do titlecase mapping on initial letters. See
            "ucfirst" for that.

            If EXPR is omitted, uses $_.

            This function behaves the same way under various pragma, such as
            in a locale, as "lc" does.

作者: seufy88    时间: 2014-04-03 13:42
回复 11# 104359176


    废除$_,那之前的代码是否不兼容阿。
作者: zhlong8    时间: 2014-04-03 14:24
不建议的是 my $_,这是个错误的实践,并不是要废除默认参数的用法。 5.18 文档中
Lexical $_ is now experimental

Since it was introduced in Perl v5.10, it has caused much confusion with no obvious solution:

    Various modules (e.g., List::Util) expect callback routines to use the global $_. use List::Util 'first'; my $_; first { $_ == 1 } @list does not work as one would expect.
    A my $_ declaration earlier in the same file can cause confusing closure warnings.
    The "_" subroutine prototype character allows called subroutines to access your lexical $_, so it is not really private after all.
    Nevertheless, subroutines with a "(@)" prototype and methods cannot access the caller's lexical $_, unless they are written in XS.
    But even XS routines cannot access a lexical $_ declared, not in the calling subroutine, but in an outer scope, iff that subroutine happened not to mention $_ or use any operators that default to $_.

It is our hope that lexical $_ can be rehabilitated, but this may cause changes in its behavior. Please use it with caution until it becomes stable.





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2