【发布时间】:2013-12-17 20:17:45
【问题描述】:
我是 perl 新手,正在尝试用 perl tk 编写程序。我有大部分,但似乎无法得到最后一点。
我正在阅读一个看起来像这样的 .txt 文件...
display ("Blue")
....
....
....
....
display ("Yellow")
....
bobby
....
....
display ("Red")
....
.... and so on
“....”是我并不真正关心的数据部分,但我必须搜索才能找到“bobby”。
我已经建立了一个数组来存储所有带有显示的行。即@array。我想在原始 .txt 文件中包含“显示”的每一行之间搜索“鲍比”,并输出其上方的任何显示行。
对于上面的例子,我想要一个
的输出显示(“黄色”)
我认为我需要一个 while 循环来在我想要搜索的显示之间的所有区域之间进行迭代。
下面的代码显示了我构建的一些内容。 输入完成后按下按钮时,将运行以下子程序。
#Output Substation ID to Pane
sub find_substations
{
#Copy the entry
my $substation_to_search = $find_ensub->get();
#Insert the copied entry into the outptutext_box
$output_textbox->insert("end", "Substation ID: $substation_to_search\n\n");
open(myfile1, "file and file location");
my @lines = <myfile1>;
#Open File and Print all lines with display in it.
my @array= grep(/display /,@lines);
#Array Size
my $size = @array;
my $start = $lines[0];
my $stop = $lines[1];
while("$x" < "$size")
{
if("$start".."$stop")
{
next if /$start/||/$stop/;
if()
{
$output_textbox->insert("end", "$lines[0]");
}#if stop
}#If stop
$x++;
}
#close myfile1;
}
我一直在阅读有关启动和停止的信息,但无法使其正常工作。谁能帮我吗?启停部分的代码不完整。
【问题讨论】:
-
对于初学者,不要不必要地在变量周围加上引号。当您尝试将它们视为数字时,这也可能会咬您:
"$x" < "$size"