|
i was pretty drunk when i posted that...
here's some slightly revised and WELL commented code
[php]#!C:\perl\bin\perl -w
&alphasort("ben","roy","zzzz","joe","john","gary","rembrandt","da vinci","jesus","ben","benjamin","roy","steph");
#returns the first name (by alpha order) passed
sub firstName
{
#snatch first value in param array
my($fname) = shift(@_);
#compare the current value to the next one
#if it's >= change, else go to the next value
foreach (@_)
{
$fname=$fname ge $_ ? $_ : next;
}
return($fname);
} #end firstname
#/////////////////////////////////////////////////////////
#takes an array of names,
#sorts them by name and number
#alphabetivcally into an associative array
#and prints it out
sub alphasort
{
#get how many parameters were passed
$numVals = @_;
#make my own copy of the array passed
my(@names) = @_;
#my hash
my(%namelist);
#loop through and fill hash
for($i=1; $i<=$numVals; $i++)
{
#call firstname and store result in currentname
$currentname = &firstName(@names);
#assign that name to the current key position
#in the hash
$namelist{$i} = $currentname;
#this part could be better...
#go through names array and replace the
#values matching the last name put into
#the hash with a bunch of z's
foreach (@names)
{
#if a match is found...
if(/$currentname/i)
{
#then replace it with z's
s/$currentname/zzzzz/i;
#then jump out, so multiple same names
#can be passed to the function
last;
}
}
}
#print the hash
while (($key, $value) = each %namelist)
{
print("$key....$value\n");
}
}[/php]
__________________
i put on my robe and wizard hat...
Have you ever heard of Plato, Aristotle, Socrates?...Morons.
|