作者Neisseria (Neisseria)
看板Perl
标题Re: [问题] 次数计算
时间Thu Nov 27 16:04:31 2014
※ 引述《b13b46o2 (买枪买装备)》之铭言:
: $ANS=1234 ;
: $count = $ANS =~ tr/1234// ;
: print "$count\n";
: 请问tr後面有办法带入自己想要的变数吗?
: google了好久
: $ANS=1234 ;
: print "Please enter your number.\n";
: chomp ($num=<STDIN>);
: $count = $ANS =~ tr/// ;
: print "$count\n";
: tr後面该怎麽改才有办法让$num带入
简单地说,请用 eval
其实 Perl 官方文件就有说明了
Because the transliteration table is built at compile time,
neither the SEARCHLIST nor the REPLACEMENTLIST are subjected to
double quote interpolation. That means that if you want to use
variables, you must use an eval():
eval "tr/$oldlist/$newlist/";
die $@ if $@;
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 203.71.94.31
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/Perl/M.1417075475.A.149.html
1F:推 b13b46o2: 我在发完文章後有看到这个有用过 但是不行... 11/27 16:17
2F:→ b13b46o2: 成功了 $count = $ANS =~ tr/// ;原来要直接换掉 11/27 16:59
3F:→ b13b46o2: 刚开始一直打在=~後面所以不行 11/27 17:00
※ 编辑: Neisseria (60.251.46.166), 12/24/2018 16:20:31