Wednesday, November 4, 2009

Find N longest lines in a multiple line text file using PHP

Find N longest lines in a multiple line text file using PHP

#!/usr/bin/php

// A program to read a multiple line text file and write the N longest lines to a new file.
// Where N and the file to be read are specified on the command line.
// Author : Akash Agrawal

// Check the number of command line arguments
$arrsize = sizeof($argv);

if($arrsize != 3)
{
echo "Invalid command line argument\n";
echo "Usage: php countMax.php (top N max size lines) (input filename)\n";
echo "Example: php countMax.php 3 input.txt\n";
exit(0);
}

// if number of arguements is right, check if arguments are entered correctly
// else exit

$N = $argv[1];
if(!is_numeric($N))
{
echo "Invalid command line argument\n";
echo "Usage: php countMax.php (top N max size lines) (input filename)\n";
echo "Example: php countMax.php 3 input.txt\n";
exit(0);
}
$filename = $argv[2];
$fh = fopen($filename, 'r') or exit("can't open file($filename)");


// read file into array along with size of each line
$countLineArray = array("");
$fileArray = array("");
$counter = 0;
if($fh)
{
while(!feof($fh))
{
$temp = fgets($fh);
$fileArray[$counter] = $temp;
$countLineArray[$counter++] = strlen($temp);
}
fclose($fh);
}

// index array to store the index of sorted size of lines of file
$countLineArray[$counter-1]++;
$indexArray = array("");
for($i=0; $i < $counter; $i++)
{
$indexArray[$i] = $i;
}

// bubble sort to sort the array and also store the index
// in indexarray
// I did'nt use the php sort function because I also wanted to save the index
for($x = 0; $x < $counter; $x++)
{
for($y = 0; $y < $counter; $y++)
{
if($countLineArray[$x] > $countLineArray[$y])
{
$hold = $countLineArray[$x]; $ihold = $indexArray[$x];
$countLineArray[$x] = $countLineArray[$y]; $indexArray[$x] = $indexArray[$y];
$countLineArray[$y] = $hold; $indexArray[$y] = $ihold;
}
}
}

if($counter < $N + 1)
$N = $counter-1;
// now since we have the indexes in indexarray, it is easy to print the first N lines
for($i=0; $i < $N; $i++)
{
echo $fileArray[$indexArray[$i]]." ".$indexArray[$i]."\n";
}

?>

No comments:

Post a Comment