Wednesday, March 10, 2010

Sorting Example

#!/usr/bin/perl -w
use strict;
use diagnostics;

print "\n";
print "+-----------------------------------------------------------------+ \n";
print "| uniqsort.pl: | \n";
print "| This program will list the unique values found in the column(s) | \n";
print "| specified by the user from the file named. The output is sorted | \n";
print "| and shows a record count per value. | \n";
print "| | \n";
print "| Note: Type in the case (upper or lower) that the filename in. | \n";
print "| ---- | \n";
print "+-----------------------------------------------------------------+ \n\n";
#- 1 --------------------------------------------------------------------------
print "Directory Name: ('.' = Current) \t" ;# Ask for directory
my $Directory = <STDIN>; # Store what's typed in
chomp($Directory); # Get rid of line break

chdir($Directory) # change to the directory
or die "Invalid Directory \n"; # or if invalid, die
#- 2 --------------------------------------------------------------------------
print "Input File: \t\t\t\t"; # Ask for filename
my $InputFile = <STDIN>; # Store what's typed in
chomp($InputFile); # Get rid of line break

open( INPUT, $InputFile) # Open the file if found
or die "The file $InputFile could not be found. \n";
#- 3 --------------------------------------------------------------------------
print "Start Position: \t\t\t"; # Ask for the beginning column
my $StartPosition = <STDIN>; # Store what's typed in
chomp($StartPosition); # Get rid of line break
$StartPosition = $StartPosition -1; # Starts from 0 so subtract 1
#- 4 --------------------------------------------------------------------------
print "End Position: \t\t\t\t"; # Ask for the ending column
my $EndPosition = <STDIN>; # Store what's typed in
chomp($EndPosition); # Get rid of line break
$EndPosition = $EndPosition -1; # Starts from 0 so subtract 1

my $ValueLength = $EndPosition
- $StartPosition + 1; # Calculate the length for substr
#- 5 --------------------------------------------------------------------------
my $InCount = 0; # Initialize the counters
my @UniqVals = (""); # and variable lists
my @UniqCnts = (0);
my @SortList = ("");
#- 6 --------------------------------------------------------------------------
READ: # Read through the input file.
while(<INPUT>) # Grab the value in the columns
{ my $TheLine = $_; # Save the line's contents
chomp($TheLine); # Get rid of the line break
$InCount = $InCount + 1; # increment input count
my $SubVal = substr($TheLine
,$StartPosition
,$ValueLength);
my $ListNum = 1; # start at beginning of existing list
#- 6.1 ----------------------------------------------------------------------
# If our counter is greater than the number of values already in the list,
# add the value to the end of one list and start the counter in the other.
#----------------------------------------------------------------------------
CHECKNUM:
if ($ListNum > $#UniqVals) # if we're at the end of the list...
{ push(@UniqVals, $SubVal); # add this value to the list
push(@UniqCnts, '1'); # increment a list of counts
goto READ; # go back and get another record
}
#- 6.2 ----------------------------------------------------------------------
if ($SubVal eq $UniqVals[$ListNum]) # If this new value is found on the list,
{$UniqCnts[$ListNum] = # increment the associated counter.
$UniqCnts[$ListNum] +1 ; # bump the counter
goto READ; # go get another record
}
#- 6.3 ----------------------------------------------------------------------
$ListNum = $ListNum + 1; # If value not on list, increment list
goto CHECKNUM; # go back for next one
}; # End of while(<INFILE>) loop
#- 7 --------------------------------------------------------------------------
print "\n";
print "****************************** \n"; # All finished, so print results
print "* $0 COMPLETED SUCCESSFULLY \n";
print "****************************** \n\n";
print "# Record: $InputFile: $InCount\n";

#- 8 --------------------------------------------------------------------------
# Start at the beginning of the two arrays and rebuild it in sorted order.
# Move the Unique values and counts to the new SortList.
#------------------------------------------------------------------------------
my $ListNum = 1; # start at beginning of 2 arrays
until ($ListNum > $#UniqVals) # til we get to the end of the array
{ push(@SortList, $UniqVals[$ListNum].' - '.$UniqCnts[$ListNum] );
$ListNum = $ListNum + 1; # increment out counter for UniqVals
};
#- 9 --------------------------------------------------------------------------
print join("\n", sort(@SortList)); # Print the sorted list
print "\n";

No comments:

Post a Comment