I am working with a text file that contains data in a format like so:
To Kill A Mocking Bird|Harper Lee|S1|4A
Life of Pi|Yann Martel|S3|5B
Hunger Games|Suzzanne Collins|S2|2C
The actual data file has many more entries, and there are more than 3 instances of S1
.
I am writing a program in Perl to compare the data in this file with another file, mainly the filing information like S1
, 4A
.
I approached this by first storing the data from the file into a string. I then split the string by using pipe |
as a delimiter and stored it into an array. I then used a foreach
loop to iterate through the array to find matching information.
Note that all files are in the same directory.
#!/usr/bin/perl
open(INFO, "psychnet3.data");
my $dbinfo = <INFO>;
close(INFO);
@dbarray = split("|", $dbinfo);
$index_counter = 0;
foreach $element (@dbarray) {
if ($element =~ "S1") {
open(INFO, ">>logfile.txt");
print INFO "found a S1";
close(INFO);
if ($dbarray[$index_counter + 1] =~ "4A") {
$counter++;
open(INFO, ">>logfile.txt");
print INFO "found S1 4A";
close(INFO);
}
}
$index_counter++;
}
In the output file, it does not find all instances of S1
.
I also tried using eq
as a conditional instead of =~
and still no luck.
I am new to Perl, coming from C#, is there any syntax I'm making a mistake with, or is it a logic error?