For an example I am reading file this where after some words I check how many "you" are present.
Good
morning
to
you
May
every
step
you
make
you
be
filled
you
with
happiness
love
you
and
peace
you
Code I wrote:
use warnings;
use strict;
my $log1_file = "log.log";
my $you_count = 0;
my $you_make_count = 0;
my $you_love_count = 0;
my $point ;
open(IN1, "<$log1_file" ) or die "Could not open file $log1_file: $!";
while (my $line = <IN1>) {
$point =$.;
print "$. main while\n";
my @fields = split' ',$line;
if ($fields[0] eq "Good") {
print "$. after good_if\n";
good_check();
print "$. after good_call\n";
seek (IN1,$point,0);
#$. = $point;
print "$. \n";
}
elsif ($fields[0] eq "make") {
print "$. after make_if\n";
make_check();
#$. = $point;
seek (IN1,$point,0);
}
elsif ($fields[0] eq "love") {
print "$. after love_if\n";
love_check();
#$. = $point;
seek (IN1,$point,0);
}
}
print "$you_count\n";
print "$you_make_count\n";
print "$you_love_count\n";
close IN1;
sub love_check{
while (my $line = <IN1>)
my @fields = split' ',$line;
if ($fields[0] eq "you") {
$you_love_count++;
}
}
}
sub make_check{
while (my $line = <IN1>) {
my @fields = split' ',$line;
if ($fields[0] eq "you") {
$you_make_count++;
}
}
}
sub good_check{
while (my $line = <IN1>) {
my @fields = split' ',$line;
if ($fields[0] eq "you") {
$you_count++;
}
}
}
If I use seek (IN1,$point,0); to point back to location I am getting output like below:
1 main while
1 after good_if
20 after good_call
20
21 main while
22 main while
23 main while
24 main while
25 main while
26 main while
27 main while
28 main while
29 main while
29 after make_if
41 main while
42 main while
43 main while
44 main while
44 after make_if
56 main while
Use of uninitialized value $fields[0] in string eq at check.pl line 15, <IN1> line 56.
Use of uninitialized value $fields[0] in string eq at check.pl line 25, <IN1> line 56.
Use of uninitialized value $fields[0] in string eq at check.pl line 33, <IN1> line 56.
57 main while
58 main while
59 main while
60 main while
61 main while
62 main while
63 main while
63 after love_if
68 main while
69 main while
70 main while
70 after love_if
75 main while
76 main while
76 after love_if
81 main while
82 main while
82 after love_if
87 main while
Use of uninitialized value $fields[0] in string eq at check.pl line 15, <IN1> line 87.
Use of uninitialized value $fields[0] in string eq at check.pl line 25, <IN1> line 87.
Use of uninitialized value $fields[0] in string eq at check.pl line 33, <IN1> line 87.
88 main while
89 main while
90 main while
91 main while
6
8
8
The "you" values at final are correct but not getting lines numbers as normal.
And if I use $. = $point; only first sub is working fine.
Can any one tell me best way to point back same location?