Re: [問題] 除了關鍵字,其餘改成大寫
謝謝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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 3 之 3 篇):
Perl 近期熱門文章
PTT數位生活區 即時熱門文章