View Single Post
Old Jun 1st, 2006, 3:28 AM   #1
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
hash namesort::easy for perl buffs i'm sure

this program is stupid and gay, but i wnat constrctive criticism of what could be done more efficiently...

#!C:\perl\bin\perl -w

&alphasort("ben","bill","joe","john","gary","roy","steph");

sub firstName
{
    my($fname) = shift(@_);
    foreach (@_)
    {
        $fname=$fname ge $_ ? $_ : next;
    }
    return($fname);
}

sub alphasort
{
    $numVals = @_;
    my(@names) = @_;
    my(%namelist);
    
    for($i=1; $i<=$numVals; $i++)
    {
        $currentname = &firstName(@names);
        $namelist{$i} = $currentname;
        
        foreach (@names)
        {
            s/$currentname/zzzzzzzzzzzzzzzz/i;
        }
    }
    
    while (($key, $value) = each %namelist)
    {
        print("$key....$value\n");
    }
}

it calls alphasort which then will call a function that returns the smallest name in the array, sticks that name into the current loop var, then it replaces that name (and unfortunately all other identical without regard to case) with a bunch of z's. it places these values into an associative array which it then prints out.

some questions:

1-better way than making a bunch of z's?
(trying not to use external functions)

2-why does the hash print out like this?

6,4,1,3,7,2,5 ??????

seems like it starts on one end and is bouncing back and forth until it hits the center??? what's the point of this? (i'm sure there's a good one).
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote