[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] Processing file backwards?



There is a fairly easy way to do this.  I read about it in some book
years ago.  It goes like this:

1. First read through the file.  As you go, note how many characters
you've read in per line.

2. Store the locations of the beginning of each line in an array.  Like
so:

	[0][0] = 0;	#	Starting location.
	[0][1] = 52;	#	Length of line #1.
	[1][0] = 53;	#	Start of line #2
	[1][1] = 5;	#	Length of line #2.
	etc....

3. You should now have a list going from zero to X.  Reverse this list.

4. Open the file as if it were a random access file.  Start moving to
each of the locations you previously found and reading the lines in.

Example:

	@fileInfo = ();
	$fileInfo[++$#fileInfo][0] = 0;
	open( THEFILE, "myFile.dat" ) || die $!;
	while( <THEFILE> ){
		$fileInfo[$#fileInfo][1] = length();
		$fileInfo[++$#fileInfo][0] =$fileInfo[$#fileInfo-1][0]+
			$fileInfo[$#fileInfo-1][1];
		}

	close( THEFILE );
	@newInfo = reverse @fileInfo;
	undef @fileInfo;

	sysopen( THEFILE, "myFile.dat" ) || die $!;
	while( $#newInfo > -1 ){
		sysseek( THEFILE, $newInfo[$#newInfo][0], 0 );
		sysread( THEFILE, $theLine, $newInfo[$#newInfo][1] )
#
#	Do whatever
#
		}

	close( THEFILE );
	exit( 0 );

I'm doing this off the top of my head but the idea should work fine.

--------------------------------------------------------------------------------
All e-mail needs to be sent to mark@cheers.jsc.nasa.gov.  If you don't,
it will
probably bounce.  What man does not understand or fears; he ultimately
destroys.
Steve Wright: Black holes are where God divided by zero.

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch