A
Alex
Here is my perl script:
my %files;
my $filepath = "test.txt";
open (IN, $filepath);
while (<IN>) {
chomp;
my $file = $_;
$files{$file} = $file;
}
close IN;
my $code = 10;
if (exists $files{$code}) {
print "find key\n";
}
exit;
here is test.txt file:
10
20
30
When I run the script, there is no print "find key". However, if I change
the scipt to:
my %files;
my $filepath = "test.txt";
open (IN, $filepath);
while (<IN>) {
chomp;
my $file = substr($_,0,2);
$files{$file} = $file;
}
close IN;
my $code = 10;
if (exists $files{$code}) {
print "find key\n";
}
exit;
It will print "find key". I could find out what's wrong. Why change
my $file = $_;
to
my $file = substr($_,0,2);
will work.
PS: in test.txt file, after type 10 followed by Enter key.
Thanks in advance!
my %files;
my $filepath = "test.txt";
open (IN, $filepath);
while (<IN>) {
chomp;
my $file = $_;
$files{$file} = $file;
}
close IN;
my $code = 10;
if (exists $files{$code}) {
print "find key\n";
}
exit;
here is test.txt file:
10
20
30
When I run the script, there is no print "find key". However, if I change
the scipt to:
my %files;
my $filepath = "test.txt";
open (IN, $filepath);
while (<IN>) {
chomp;
my $file = substr($_,0,2);
$files{$file} = $file;
}
close IN;
my $code = 10;
if (exists $files{$code}) {
print "find key\n";
}
exit;
It will print "find key". I could find out what's wrong. Why change
my $file = $_;
to
my $file = substr($_,0,2);
will work.
PS: in test.txt file, after type 10 followed by Enter key.
Thanks in advance!