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

看板Perl作者 (小a)時間15年前 (2010/04/23 19:54), 編輯推噓0(001)
留言1則, 1人參與, 最新討論串2/3 (看更多)
※ 引述《iloveyoungae (迷上西方天使)》之銘言: : 各位先輩大家好 : 小弟最近開始練習一個題目 : 讀一個檔案,將關鍵字以外的字串,都變成大寫 : 這是小弟的寫法 : 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後面,應該要變成大寫,卻還是跟原本一樣,沒有改變 : 請教 這個問題該怎麼處理 : 謝謝 #!/usr/bin/perl while(<DATA>) { # s/\w+/($& eq 'input' or $& eq 'module') ? $& : uc($&)/ge; foreach $s (/\w+/g) { $s=~/input|module/i or s/$s/uc($s)/e; } print; } __DATA__ 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; -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.192.170.66 ※ 編輯: giacch 來自: 123.192.170.66 (04/23 20:13)

05/03 09:11, , 1F
謝謝G大的幫忙
05/03 09:11, 1F
文章代碼(AID): #1BqOfkAJ (Perl)
文章代碼(AID): #1BqOfkAJ (Perl)