2011-07-04 16 views
1

我有一个包含大量数据的像一个文件:找到具体的一条线,其下的{}(TCL)可

########################################################## 
World is great 
world has lots of counties 
I am john who is staying in world 
World { 
abcd123 { 
how are you 
} 

abcd456 { 
how is life 
} 

abcd789 { 
what are you doing 
} 

} 
Hey going for moive 
The life in this world is superb 
############################################# 

我想“世界”只喜欢 我想在目前的信息输出为:

abcd123 { 
how are you 
} 

abcd456 { 
how is life 
} 

abcd789 { 
what are you doing 
} 
只有

+0

没有时间好好回答,但'信息complete'将是有益的。 –

+0

这类任务通常通过编写一个解析器来解决,该解析器逐个字符地扫描输入并维护状态机。对于这个任务,解析器不会很复杂。 – kostix

+0

你怎么知道第一行(以“World”开头)并不重要,但第四行(以“World {”开头)是? – RHSeeger

回答

1
set f [open filename r] 
set contents [read -nonewline $f] 
close $f 

if {[info complete $contents]} { 
    # safe to treat string as a list 
    for {set i 0} {$i < [llength $s] - 1} {incr i} { 
    if {[lindex $s $i] eq "World" && [llength [lindex $s $i+1]] > 1} { 
     puts [lindex $s $i+1] 
    } 
    } 
} 

或者,也许这是一样的东西多纳尔的建议

set f [open $filename r] 
set collecting false 
set data "" 
while {[gets $f line] != -1} { 
    if {[regexp {^\s*World\s+\{\s*$} $line]} { 
    set collecting true 
    } 
    if {$collecting} { 
    append data $line \n 
    if {[info complete $data]} break 
    } 
} 
close $f 
puts [lindex $data 1] 
+0

我的问题是,虽然我新的如何解析线条,我不知道如何确定哪个是重要的。那个信息从未被发现...... –

+0

确实。昨天我正在考虑我的思想导师帽。 –