#!/usr/bin/perl -w use strict; my $NEWLINE_SIZE = length "\n"; # The size of the newline "character" in this OS my $OS_ADJUST = 1; # A way to somewhat generically do OS-specific offset computation my $KEY_OFFSET = 'O'; # Optimized key name for offset value my $KEY_LENGTH = 'L'; # Optimized key name for length value my $SEEK_SET = 0; # In case you don't want to export the constant for seek() my $Inpfnm = 'test2.dat'; my $Wrkfnm = $Inpfnm . '-presort.dat'; my $Srtfnm = $Inpfnm . '-sorted.dat'; my $Outfnm = $Inpfnm . '-output.dat'; { &sortFile(); &cleanUp(); } exit; sub sortFile { # In the old days this would also be known as shakeTheHardDrive() open INPUT_FILE, '<', "$Inpfnm"; binmode INPUT_FILE; open OUTPUT_FILE, ">$Outfnm"; open SORTED_FILE, "<$Srtfnm"; while (my $sortedKeyBuffer = ) { chomp $sortedKeyBuffer; my ($keyValue, $inputOffset, $workingLength) = split /\|/, $sortedKeyBuffer; seek INPUT_FILE, $inputOffset, $SEEK_SET; my $inputBuffer = ''; my $inputCount = read INPUT_FILE, $inputBuffer, $workingLength; print OUTPUT_FILE "$inputBuffer\n"; } close SORTED_FILE; close OUTPUT_FILE; close INPUT_FILE; } sub cleanUp { unlink $Wrkfnm; unlink $Srtfnm; } __END__