Performance 如何在perl中执行此文件操作?
因此,我的文件如下所示:Performance 如何在perl中执行此文件操作?,performance,perl,file-io,Performance,Perl,File Io,因此,我的文件如下所示: --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
--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
我想做什么:从名称字段中获取名称,查看它是否匹配我们要搜索的名称之一(已在数组中提供),然后获取路径,转到该路径,执行一些操作并获取新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
}
}
}
@filedata=#读取数组中的文件
$names_to_search=join(“|”,@some_name);
while(行=@filedata)
{
如果($line=~/comp:/)
{
$line=;
如果($line=~/name:$names\u to\u search/)
{
#循环直到找到id为止
#记住这个索引,因为我们需要更改这个id
#循环直到找到路径字段
#获取路径,转到该路径,执行一些perforce命令并获取新id
如果(id与当前id相同),则无需执行任何操作
否则,用新id替换当前id
}
}
}
问题:我当前的实现有三个while循环!有没有更好的/高效的/优雅的方法 您已经以自定义格式编写了一个配置文件,然后尝试手动解析它。相反,为什么不以YAML或INI等既定格式编写文件,然后使用现有模块对其进行解析 例如,使用YAML:
use YAML::Any;
my @data = YAML::Any::LoadFile($filename) or die "Could not read from $filename: $!":
# now you have your data structure in @data; parse it using while/for/map loops.
您可以使用或读取INI文件。您已经以自定义格式编写了配置文件,然后尝试手动解析它。相反,为什么不以YAML或INI等既定格式编写文件,然后使用现有模块对其进行解析 例如,使用YAML:
use YAML::Any;
my @data = YAML::Any::LoadFile($filename) or die "Could not read from $filename: $!":
# now you have your data structure in @data; parse it using while/for/map loops.
您可以使用或读取INI文件。以下是一些伪代码:
index = 0;
index_of_id = 0; // this is the index of the line that contains the current company id
have_company = false; // track whether we are processing a copmany
while (line in @filedata)
{
if (!have_company)
{
if (line is not "company")
{
++index;
continue;
}
else
{
index_of_id = 0;
have_company = true;
}
}
else
{
if (line is "end_comp")
{
have_company = false; // force to start looking for new company
++index;
continue;
}
if (line is "id")
index_of_id = index; // save the index
if (line is "path")
{
// do your stuff then replace the string at the index given by index_of_id
}
}
// line index
++index;
}
// Now write the modified array to file
下面是一些伪代码:
index = 0;
index_of_id = 0; // this is the index of the line that contains the current company id
have_company = false; // track whether we are processing a copmany
while (line in @filedata)
{
if (!have_company)
{
if (line is not "company")
{
++index;
continue;
}
else
{
index_of_id = 0;
have_company = true;
}
}
else
{
if (line is "end_comp")
{
have_company = false; // force to start looking for new company
++index;
continue;
}
if (line is "id")
index_of_id = index; // save the index
if (line is "path")
{
// do your stuff then replace the string at the index given by index_of_id
}
}
// line index
++index;
}
// Now write the modified array to file
由于没有两个块具有相同的
名称
值,因此可以使用哈希引用的哈希引用:
{
"name1"=>{type=>"type1",id=>"id1",owner=>"owner1",path=>"path1"},
"name2"=>{type=>"type2",id=>"id2",owner=>"owner2",path=>"path2"},
#etc
}
类似的方法应该可以工作(警告:未测试):
使用严格;
使用警告;
open(my$read,“由于没有两个块具有相同的名称
值,因此可以使用哈希引用的哈希引用:
{
"name1"=>{type=>"type1",id=>"id1",owner=>"owner1",path=>"path1"},
"name2"=>{type=>"type2",id=>"id2",owner=>"owner2",path=>"path2"},
#etc
}
类似的方法应该可以工作(警告:未测试):
使用严格;
使用警告;
打开(my$read,“文件是否可以包含两个具有相同值的块,用于name
”?文件是否可以包含两个具有相同值的块,用于name
?正如很多人建议的那样,我将以xml格式编写它,然后使用Perl的xml解析器。:)正如很多人建议的那样,我将以xml格式编写它,然后使用Perl的xml解析器。:)