Perl:为什么 "seek" 不起作用
Perl: How come "seek" is not working
我正在尝试浏览一堆文本文件两次以查找两个不同的值。但是,seek $fh, 0, 0
似乎不起作用。为什么?
请帮忙
我的代码:
use strict;
use warnings;
...
read_in_data_employer();
read_in_data_union();
process_files ($FileFolder);
close $FileHandle;
...
sub process_files
{
opendir (DIR, $FileFolder)
or die "Unable to open $FileFolder: $!";
my @files = grep { /.pdf.txt/ } readdir (DIR);
closedir (DIR);
@files = map { $FileFolder . '/' . $_ } @files;
foreach my $file (@files)
{
open (my $txtfile, $file) or die "error opening $file\n";
print "$file";
LookForEmployer:
{
print $FileHandle "\t";
while (my $line=<$txtfile>)
{
foreach (@InputData_Employers)
{
if ($line =~ /\Q$_/i)
{
print $FileHandle "$_";
last LookForEmployer;
}
}
}
}
seek ($txtfile, 0, 0);
LookForUnion:
{
print $FileHandle "\t";
while (my $line=<$txtfile>)
{
print "$.\n";
foreach (@InputData_Unions)
{
if ($line =~ /\Q$_/i)
{
print $FileHandle "$_";
last LookForUnion;
}
}
}
}
close $txtfile
}
}
输出:
>perl "test.pl" test "employers.txt" "unions.txt" output.txt
test/611-2643-03 (801-0741).pdf.txt12
13
14
15
16
17
18
19
20
21
22
test/611-2643-05 (801-0741).pdf.txt
7
8
9
10
11
12
test/611-2732-21 (805-0083).pdf.txt
2
3
4
5
6
7
8
test/611-2799-17 (801-0152).pdf.txt
6
7
8
9
10
11
12
13
14
谢谢
文件没有行号。他们甚至没有台词。文件只有字节。这意味着您不能只询问系统 "What line of the file is at this position?"
但是,由于您正在查找文件的开头,因此您只需重置 $.
。
use Fcntl qw( SEEK_SET );
seek($txtfile, 0, SEEK_SET)
or die("seek: $!\n");
$. = 0;
顺便说一下,你的程序效率低得离谱。将数据加载到哈希或数据库中!
我正在尝试浏览一堆文本文件两次以查找两个不同的值。但是,seek $fh, 0, 0
似乎不起作用。为什么?
请帮忙
我的代码:
use strict;
use warnings;
...
read_in_data_employer();
read_in_data_union();
process_files ($FileFolder);
close $FileHandle;
...
sub process_files
{
opendir (DIR, $FileFolder)
or die "Unable to open $FileFolder: $!";
my @files = grep { /.pdf.txt/ } readdir (DIR);
closedir (DIR);
@files = map { $FileFolder . '/' . $_ } @files;
foreach my $file (@files)
{
open (my $txtfile, $file) or die "error opening $file\n";
print "$file";
LookForEmployer:
{
print $FileHandle "\t";
while (my $line=<$txtfile>)
{
foreach (@InputData_Employers)
{
if ($line =~ /\Q$_/i)
{
print $FileHandle "$_";
last LookForEmployer;
}
}
}
}
seek ($txtfile, 0, 0);
LookForUnion:
{
print $FileHandle "\t";
while (my $line=<$txtfile>)
{
print "$.\n";
foreach (@InputData_Unions)
{
if ($line =~ /\Q$_/i)
{
print $FileHandle "$_";
last LookForUnion;
}
}
}
}
close $txtfile
}
}
输出:
>perl "test.pl" test "employers.txt" "unions.txt" output.txt
test/611-2643-03 (801-0741).pdf.txt12
13
14
15
16
17
18
19
20
21
22
test/611-2643-05 (801-0741).pdf.txt
7
8
9
10
11
12
test/611-2732-21 (805-0083).pdf.txt
2
3
4
5
6
7
8
test/611-2799-17 (801-0152).pdf.txt
6
7
8
9
10
11
12
13
14
谢谢
文件没有行号。他们甚至没有台词。文件只有字节。这意味着您不能只询问系统 "What line of the file is at this position?"
但是,由于您正在查找文件的开头,因此您只需重置 $.
。
use Fcntl qw( SEEK_SET );
seek($txtfile, 0, SEEK_SET)
or die("seek: $!\n");
$. = 0;
顺便说一下,你的程序效率低得离谱。将数据加载到哈希或数据库中!