I open a text report inside my Perl script and need to find the specific lines and store them in arrays.
this is my report which I need to process through:
matched pattern 1
line1:10
line2:20
line3:30
next matched pattern 2
line1:5
line2:10
line3:15
next matched pattern 3
lineA:A
lineB:B
lineC:C
.
.
------------------------------------
this part is my script:
@numbers;
@numbers2;
@letters;
while (<FILE>)
{
if ($_ =~/matched pattern 1/ && $_ ne "\n")
{
chomp();
push (@numbers,$_)
}
if ($_ =~/next matched pattern 2/ && $_ ne "\n")
{
chomp();
push (@numbers2,$_)
}
if ($_ =~/next matched pattern 3/ && $_ ne "\n")
{
chomp();
push (@letters,$_)
}
}
then I can use numbers and letters inside the arrays.
this is a part of my report file
Maximum points per Lab
Lab1:10
Lab2:30
Lab3:20
Maximum points per Exam
Exam1:50
Exam2:50
Maximum points on Final
Final:150
What is your program supposed to be doing? Your current program is looking for the lines that have
matched patternand storing THOSE VERY LINEs into three different arrays. All other lines are ignored.You show some sort of example output, but there’s no real relationship between your output and input.
First, learn about references, so you don’t need five different arrays. In my example, I use an array of arrays to store all of your separate files. If each file represents something else, you could use an array of hashes or a hash of arrays or a hash of hashes of arrays to represent this data in a unified structure. (Don’t get me started on how you really should learn object oriented Perl. Get the hang of references first).
Also get a book on modern Perl and learn the new Perl syntax. It looks like your Perl reference is for Perl 4.0. Perl 5.0 has been out since 1994. There’s a big difference between Perl 4 and Perl 5 in the way syntax is done.
OUTPUT. Each set of lines are in a different array: