Perl 变量不会保持设置,即使设置为 "our"
Perl variable doesn't stay set, even when set with "our"
我有一个文件,我希望搜索字符串,与存储在数组中的项目相匹配。我打开文件,遍历它,当我找到我要找的东西时,我更改了一个变量标志,以便可以完成其他工作。我遇到的问题是我设置的变量没有保留。我将变量放在 foreach 循环中,使其成为 our
变量,但无济于事。
有问题的代码:
my $zed = 0;
foreach my $SUB (@sub) {
print "We are currently looking for $SUB<br><br>";
open (my $input, "<", "C:\Users\scottbro\Desktop\PMS.txt")
or die "Cannot open PMS.txt: $!";
our $flag = 0;
##SET Zed to keep track of where we are
print "Zed is $zed<br>Flag is $flag<br>";
$zed++;
while (<$input>) {
print "inside the while loop, flag is now $flag<br>";
## IF you find what you are looking for, set flag to 1
if (/$SUB/) {
$flag = 1;
print "Found sub property, flag is now $flag<br>";
## IF flag is 1, and line has email address, show it!
} elsif ($flag = 1 && /<email>(.+)/) {
print "Flag is $flag, email is <br>";
}
}
close($input);
}
输出,可以看到flag变量丢失值:
We are currently looking for Property 1
Zed is 0
Flag is 0
inside the while loop, flag is now 0
inside the while loop, flag is now
Flag is 1, email is email1
inside the while loop, flag is now 1
Flag is 1, email is email2
inside the while loop, flag is now 1
inside the while loop, flag is now
inside the while loop, flag is now
inside the while loop, flag is now
Found sub property, flag is now 1
inside the while loop, flag is now 1
Flag is 1, email is email3
inside the while loop, flag is now 1
Flag is 1, email is email4
inside the while loop, flag is now 1
inside the while loop, flag is now
inside the while loop, flag is now
Flag is 1, email is email 5
你正在分配给你的测试$flag
elsif ( $flag=1 && /<email>(.+)/ ) { ... }
将 $flag
设置为 1 && /<email>(.+)/
,如果正则表达式模式不匹配
,则为 false
应该是
if ( $flag and /<email>(.+)/ ) { ... }
我建议你忘掉 $flag
而是使用这样的事实,即如果 $sub
没有找到,输入文件将在 eof,所以程序将无法读取无论如何
倒回文件比重新打开文件更好。几乎可以肯定数据应该被解析成哈希并直接访问,但我不能确定你提供的少量信息
my @sub;
open my $fh, '<', 'C:\Users\scottbro\Desktop\PMS.txt' or die "Cannot open PMS.txt: $!";
for my $sub ( @sub ) {
seek $fh, 0, 0;
while ( <$fh> ) {
last if /$sub/;
}
while ( <$fh> ) {
next unless /<email>(.+)/;
print qq{Email for "$sub" is ""};
last;
}
}
您在测试中给 $flag
赋值,而不是检查它的值。
elsif ($flag = 1 && /<email>(.+)/)
应该是
elsif ($flag == 1 && /<email>(.+)/)
甚至更好,
elsif ($flag && /<email>(.+)/)
也就是说,重复读取同一个文件是非常低效的。这是一个没有的版本。
my $pat = join '|', map quotemeta, @subs;
my $qfn = 'C:\Users\scottbro\Desktop\PMS.txt';
open(my $fh, '<', $qfn)
or die(qq{"Can't open "$qfn": $!\n"});
while (<$fh>) {
my ($sub) = /($pat)/
or next;
defined( $_ = <$fh> )
or last;
my ($email) = /<email>(.+)/
or redo;
print(qq{Email for "$sub" is "$email"\n});
}
我有一个文件,我希望搜索字符串,与存储在数组中的项目相匹配。我打开文件,遍历它,当我找到我要找的东西时,我更改了一个变量标志,以便可以完成其他工作。我遇到的问题是我设置的变量没有保留。我将变量放在 foreach 循环中,使其成为 our
变量,但无济于事。
有问题的代码:
my $zed = 0;
foreach my $SUB (@sub) {
print "We are currently looking for $SUB<br><br>";
open (my $input, "<", "C:\Users\scottbro\Desktop\PMS.txt")
or die "Cannot open PMS.txt: $!";
our $flag = 0;
##SET Zed to keep track of where we are
print "Zed is $zed<br>Flag is $flag<br>";
$zed++;
while (<$input>) {
print "inside the while loop, flag is now $flag<br>";
## IF you find what you are looking for, set flag to 1
if (/$SUB/) {
$flag = 1;
print "Found sub property, flag is now $flag<br>";
## IF flag is 1, and line has email address, show it!
} elsif ($flag = 1 && /<email>(.+)/) {
print "Flag is $flag, email is <br>";
}
}
close($input);
}
输出,可以看到flag变量丢失值:
We are currently looking for Property 1
Zed is 0
Flag is 0
inside the while loop, flag is now 0
inside the while loop, flag is now
Flag is 1, email is email1
inside the while loop, flag is now 1
Flag is 1, email is email2
inside the while loop, flag is now 1
inside the while loop, flag is now
inside the while loop, flag is now
inside the while loop, flag is now
Found sub property, flag is now 1
inside the while loop, flag is now 1
Flag is 1, email is email3
inside the while loop, flag is now 1
Flag is 1, email is email4
inside the while loop, flag is now 1
inside the while loop, flag is now
inside the while loop, flag is now
Flag is 1, email is email 5
你正在分配给你的测试$flag
elsif ( $flag=1 && /<email>(.+)/ ) { ... }
将 $flag
设置为 1 && /<email>(.+)/
,如果正则表达式模式不匹配
应该是
if ( $flag and /<email>(.+)/ ) { ... }
我建议你忘掉 $flag
而是使用这样的事实,即如果 $sub
没有找到,输入文件将在 eof,所以程序将无法读取无论如何
倒回文件比重新打开文件更好。几乎可以肯定数据应该被解析成哈希并直接访问,但我不能确定你提供的少量信息
my @sub;
open my $fh, '<', 'C:\Users\scottbro\Desktop\PMS.txt' or die "Cannot open PMS.txt: $!";
for my $sub ( @sub ) {
seek $fh, 0, 0;
while ( <$fh> ) {
last if /$sub/;
}
while ( <$fh> ) {
next unless /<email>(.+)/;
print qq{Email for "$sub" is ""};
last;
}
}
您在测试中给 $flag
赋值,而不是检查它的值。
elsif ($flag = 1 && /<email>(.+)/)
应该是
elsif ($flag == 1 && /<email>(.+)/)
甚至更好,
elsif ($flag && /<email>(.+)/)
也就是说,重复读取同一个文件是非常低效的。这是一个没有的版本。
my $pat = join '|', map quotemeta, @subs;
my $qfn = 'C:\Users\scottbro\Desktop\PMS.txt';
open(my $fh, '<', $qfn)
or die(qq{"Can't open "$qfn": $!\n"});
while (<$fh>) {
my ($sub) = /($pat)/
or next;
defined( $_ = <$fh> )
or last;
my ($email) = /<email>(.+)/
or redo;
print(qq{Email for "$sub" is "$email"\n});
}