1

I tried this code.

Code:

use strict;
use warnings;
use Fcntl qw(SEEK_SET SEEK_CUR SEEK_END);

open($file1) # I didn't put full line
open (F_log)

while (my $line = <$file1>) {
    my @fields = split " ", $line;
    my $length = @fields;

    for (my $i=3; $i<5 ;$i+="1") {
        $i_d = $fields[$i];

        last if ($.==1);

        seek F_log, 0, SEEK_SET; # not working
            while(my $LINE = (<F_log>)) { 
                if ($. >= $a ) {
                    if ($LINE =~ /^\s+(\w+)\s\s"(\w+)",/) { 
                    if ($1 eq $i_d) {
                        $fields[$i] = $2;
                        last;   
                    }
                    else
                    { 
                        $fields[$i] = "Error:$i_d";

                    };
                };
            };  
        }; 
    };
}
close
close

I want to read whole <F_log> everytime their increment in loop. I tried other method of opening/closing file inside while loop and it works but I think that will increase run time memory of script(Not Sure). So now I am trying to reset the pointer to starting but it's not working. Can anyone suggest anything? And also if anyone tell what all things increases the run time memory of script. Thankyou

Jim Davis
  • 5,241
  • 1
  • 26
  • 22
Alexx
  • 475
  • 2
  • 8
  • 2
    What error do you get? (`seek(F_log,0,SEEK_SET) or die("seek: $!\n");`) – ikegami Oct 03 '20 at 20:41
  • It's likely `open (F_log)` didn't work. You need to check if all your file functions worked. See https://stackoverflow.com/questions/3203860/how-do-you-check-the-success-of-open-file-in-perl – Schwern Oct 03 '20 at 20:53
  • Your code does not compile. Could you post a [more complete example](https://stackoverflow.com/help/minimal-reproducible-example), please? – Schwern Oct 03 '20 at 20:56

1 Answers1

2

I think the actual error might be the use of $. as the line number. There's no way to know for seek to know into which line you seeked, so seek doesn't change $.. It just keeps going up.

In this particular use of seek, you know you're back before the first line. Try using $. = 0; after the seek.


Demonstration

a.pl:

use strict;
use warnings;

use Fcntl qw( SEEK_SET );

open(my $fh, "<", "a.txt") or die $!;
while (<$fh>) {
   print "$.: $_";
}

seek($fh, 0, SEEK_SET) or die $!;
$. = 0 if $ARGV[0];

while (<$fh>) {
   print "$.: $_";
}

a.txt:

a
b
c

Output:

$ perl a.pl 0
1: a
2: b
3: c
4: a
5: b
6: c

$ perl a.pl 1
1: a
2: b
3: c
1: a
2: b
3: c
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • but we write `seek($fh, 0, SEEK_SET)` this should set the pointer to starting then why we to $.=0? I didn't understand this. – Alexx Oct 04 '20 at 05:48
  • Also I my code I am reading two files using while loop one inside the another So if I write `$.=0` after seek why/how it is changing the pointer for `` and not for ``? – Alexx Oct 04 '20 at 05:51
  • There's no point in having `seek` set `$.` for one very specific set of inputs and no others. That would be the more confusing interface. – ikegami Oct 04 '20 at 05:51
  • From perlvar, "When a line is read from a filehandle (via `readline()` or `<>`), or when `tell()` or `seek()` is called on it, `$.` becomes an alias to the line counter for that filehandle." So if you seek, `$.` becomes an alias for the handle passed to `seek`, so assigning to `$.` will affects that handle's line counter. – ikegami Oct 04 '20 at 05:57