【发布时间】:2011-12-15 16:54:30
【问题描述】:
所以我的文件看起来像这样:
--some comments--
--a couple of lines of header info--
comp:
name: some_name_A
type: some_type
id: an id_1
owner: who owns it
path: path_A to more data
end_comp
comp:
name: some_name_B
type: some_type
id: an id_2
owner: who owns it
path: path_B to more data
end_comp
我想做的事:从名称字段中获取名称,看看它是否与我们要搜索的名称之一匹配(已经在数组中提供),然后获取路径,去那条路,做一些perforce的东西并获得新的id,然后用新的id替换当前的id,只有当它与当前的id不同时。
我做了什么(只是一个伪):
@filedata = <read_file> #read file in an array
$names_to_search = join("|", @some_names);
while(lines=@filedata)
{
if( $line =~ /comp:/ )
{
$line = <next line>;
if( $line =~ /name: $names_to_search/ )
{
#loop until we find the id
#remember this index since we need to change this id
#loop until we find the path field
#get the path, go to that path, do some perforce commands and obtain new id
if( id is same as current id ) no action required
else replace current id with new id
}
}
}
问题:我当前的实现有三个 while 循环!有没有更好/高效/优雅的方式来做到这一点?
【问题讨论】:
-
文件是否可以包含两个具有相同
name值的块?
标签: performance perl file-io