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

看板Perl作者 (迷上西方天使)時間15年前 (2010/04/26 18:14), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/3 (看更多)
謝謝G大的幫忙 小弟後來的寫法為 use strict; open(open_file "02.txt") or "error file!!"; open(write_file, ">04.txt") or "error file!!"; my $rest; while(<open file>) { chomp; if ($_=~ /(\binput\b|\bmodule\b)(.*$)/i) { $rest=$2; $rest=~ tr/a-z/A-Z; print write_file "$1 $rest\n"; } else $_=~ tr/a-z/A-Z; print write_file "$_","\n"; } 但是這個沒有考慮到,如果在$2裡面有keyword的情況 還是G大的寫法,比較完善 謝謝 ※ 引述《giacch (小a)》之銘言: : ※ 引述《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: 61.220.246.245
文章代碼(AID): #1BrMTzPP (Perl)
文章代碼(AID): #1BrMTzPP (Perl)