Re: [問題] 次數計算

看板Perl作者 (Neisseria)時間10年前 (2014/11/27 16:04), 6年前編輯推噓1(102)
留言3則, 1人參與, 最新討論串2/2 (看更多)
※ 引述《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://www.ptt.cc/bbs/Perl/M.1417075475.A.149.html

11/27 16:17, , 1F
我在發完文章後有看到這個有用過 但是不行...
11/27 16:17, 1F

11/27 16:59, , 2F
成功了 $count = $ANS =~ tr/// ;原來要直接換掉
11/27 16:59, 2F

11/27 17:00, , 3F
剛開始一直打在=~後面所以不行
11/27 17:00, 3F
※ 編輯: Neisseria (60.251.46.166), 12/24/2018 16:20:31
文章代碼(AID): #1KTjiJ59 (Perl)
討論串 (同標題文章)
文章代碼(AID): #1KTjiJ59 (Perl)