in reply to point to previous line

hari,
I am not exactly sure I understand what you are trying to do. You say that you want to point to the previous line, so that if you read from the file handle, it returns the same line. Returning the same line is not the same as retrieving the previous line.

Since I do not completely understand - let me offer some possible alternatives:

  • You could use the "redo" command in a while loop if you want the same line and not the previous line
  • You could read each line in and store the previous line in a buffer
  • You could mark each position using tell and seek back as has been suggested
  • You could store the file in an array as has been suggested if memory is not an issue
  • You could use Tie::File (which treats a file as an array) if memory is an issue, but speed is not

    To help you more than that - you need to explain more clearly what you are trying to do by giving a sample file and showing what you want to do at each point in the file.

    Hope this helps - cheers - L~R

    Update: While I was working on posting this, rir had already come up with the idea of temporarily storing the previous line

  • Replies are listed 'Best First'.
    Re: Re: point to previous line
    by hari (Novice) on Mar 06, 2003 at 04:48 UTC
      I am trying to parse huge files. I will be looking for a delimiter in each line. When i encounter a delimiter i
      am passing the current line and the filehandle to a subroutine to parse next few lines. But if the next
      lines are incomplete or is not what i am looking for, i have to rollback. so that the parent subroutine will
      continue checking from the line it sent to this subroutine.

      Thanks
      Hari
        hari,
        What you want to do is create a flag variable as well as tell. This should give you an idea of how to get started.

        The basic logic is this.

      • Store your position in the file with tell and read a line
      • Check to see if it contains data you are looking for
      • If no, reset the flag and move on as normal
      • If yes, check to see if your special flag is set
      • If yes, move on as normal
      • If no, go into sub-routine
      • When sub-routine returns, determine if it was succesful
      • If yes, move on as normal
      • If no, seek back to the position stored with tell, set your skip flag, use the redo command so that you do not actually read the next line in the file

        I am at work or I would actually write the code. If you need more assistance than the logic and the link to work this out, let me know and I will write it tonight when I get home.

        I hope this helps - cheers - L~R

          hi, I thought tell and seek is the best and right way to do it.
          I have done it the following way.
          sub parent { while($line = <FILE>) { if(#line =~ something) { $parsed = child(*line,*FILE); if($parsed =~ /##_XYZ_##/) { my @tempArray = split /##_XYZ_##/,$parsed; $line = "$tempArray[1]"; redo; } else { continue...further.... } } } } sub child { my *cLine = shift; my *FILE = shift; my $temp = <FILE>; if(test($temp )) { return $cLine."##_XYZ_##"."$temp"; } else { ...continue further... } }

          Thanks for the support.