[問題] 除了關鍵字,其餘改成大寫

看板Perl作者 (迷上西方天使)時間15年前 (2010/04/23 13:02), 編輯推噓0(008)
留言8則, 2人參與, 最新討論串1/3 (看更多)
各位先輩大家好 小弟最近開始練習一個題目 讀一個檔案,將關鍵字以外的字串,都變成大寫 這是小弟的寫法 use strict; open(open_file "02.txt") or "error file!!"; open(write_file, ">04.txt") or "error file!!"; while(<open file>) { chomp; if ($_ !~/input\s|module\s/i) { $_=~ tr/a-z/A-Z; } print write_file "$_","\n"; } 但是結果出來,有點問題 module wait68k (lbus , lcs_n , mode_68k , IOIS16_N , PDOE , WE_N , LBUS_N , OE_N, LOAD_N , MIIDIOEN1 , PDOE_N ); input lbus; input lcs_n; input mode_68k; input iois16_n; input pdoe; input we_n; input lbus_n; input oe_n; OUTPUT LOAD_N; OUTPUT MIIDIOEN1; OUTPUT PDOE_N; 像是module跟input後面,應該要變成大寫,卻還是跟原本一樣,沒有改變 請教 這個問題該怎麼處理 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.220.246.245

04/23 19:21, , 1F
/input\s|module\s <- 這是指「只要該行字出現input或
04/23 19:21, 1F

04/23 19:22, , 2F
module這2個字,整行字就不用全變成大寫;否則就全變
04/23 19:22, 2F

04/23 19:23, , 3F
大寫」吧?
04/23 19:23, 3F

04/23 19:23, , 4F
/input\s|module\s/i (1F沒打好)
04/23 19:23, 4F

04/23 19:39, , 5F
一種作法是先將整行字變大寫,然後把module和input變小
04/23 19:39, 5F

04/23 19:40, , 6F
另一種是將input或module和其它字分開來,然後分別處理
04/23 19:40, 6F

04/23 19:42, , 7F
Google: "Perl built-in function"找可以用的function
04/23 19:42, 7F

04/26 18:04, , 8F
謝謝C大的建議!!
04/26 18:04, 8F
文章代碼(AID): #1BqIdqsj (Perl)
文章代碼(AID): #1BqIdqsj (Perl)