作者giacch (小a)
看板Perl
标题Re: [问题] 如何取得()内不定长度的字串
时间Sun May 2 22:19:01 2010
※ 引述《iloveyoungae (迷上西方天使)》之铭言:
: 大家好
: 小弟最近在练习ㄧ个题目
: 将.lib里面的cell 取出来
: 并且依照cell名称另存新档
: 目前,已经可以成功的取出来
: 但是名称上面依旧有着(),我只想保留里面的名称
: 例如
: cell (FADDS1), 我只想要取出FADDS1而不要有()
: 但是每个名称不见得ㄧ样长度
: 不知道各位大大,有没有好的想法可以建议ㄧ下
: 谢谢
: open(open_file, "12.txt") or "error file!!";
: my $Out;
: my $name;
: while (<open_file>)
: {
: if ($_=~ m/^cell/g)
: {
: if ($_=~ /\s(.*\b.)/)
: {
: $name=$1;
: open(write_file, ">$name.txt") or "error file!!";
: }
: print $_;
: print write_file $_;
: $Skip= 1;
: next;
: }
: if ($Skip==1)
: {
: $Out .=$_;
: print $_;
: print write_file $_;
: }
: if ($_=~ m/^}/g)
: {
: $Skip=0;
: next;
: close write_file;
: }
: }
: close open_file;my $Skip;
#!/usr/bin/perl
$file='12.txt';
open(READ, $file) or die "$file: $!\n";
@DATA=<READ>;
close(READ);
foreach(@DATA) {
/cell *\((\w+)\)/i and $name=$1.".txt";
if(defined $name) {
push(@TMP, $_);
foreach(/\{/g) { $L++ };
foreach(/\}/g) { $R++ };
if($L==$R) {
print "\n#### $name ####\n".join(undef, @TMP);
open(WRITE, '>'.$name) or die "$name: $!\n";
print WRITE join(undef, @TMP);
undef @TMP;
close(WRITE);
undef $name;
}
}
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 123.192.170.66
1F:推 iloveyoungae:谢谢g大,不过好像有点问题 05/03 13:22
2F:→ iloveyoungae:显示 12.txt:No such file or directiory 05/03 13:23
3F:→ iloveyoungae:可是12.txt明明就在正确的directory下面,有点疑虑 05/03 13:24