楼主:
Neisseria (Neisseria)
2014-11-27 16:04:31※ 引述《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 $@;