![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#2 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
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. |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
this isn't intended for "finished projects", but here's the end result of my efforts.
[php]#!C:\perl\bin\perl -w #call function ////////////////////////////////////////////// &alphaSort("ben","roy","kathy","van gogh","roy","danny", "rembrandt","da vinci","jesus","ben","benjamin","roy","steph", "billy","johnny","branden","monica","asshole","butthead","beavis", "daWei","sykkn","big K","ooble","pizentios","sarumont","bl00dninja"); #////////////////////////////////////////////////////////////// #returns the first name (by alpha order) passed sub firstName { #snatch first value in paramater array my($fName) = shift; #compare the current value to the next one... #if it's >= then change it, 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) = @_; #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/zzzzzzz/i; #then jump out, so multiple same names #can be passed to the function last; }#end if }#end foreach }#end for #call function to print hash in sorted #numerical order &sortedPrint(%nameList); }#end alphaSort #/////////////////////////////////////////////////////////////// sub sortedPrint { #save hash keys into an array @keys = keys(%nameList); #sort that array numerically using perl's sort function @sortedKeys = sort{$a <=> $b} @keys; #print the key and the value foreach pair foreach (@sortedKeys) { print "$_\t...$nameList{$_}\n"; } } #end all ////////////////////////////////////////////////////[/php] chose the php option b/c it highlights comments. why did i not use sort earlier? i wanted to do things "the hard way" and play with perl a bit. why did i not utilize some better spacing? because i love the obfuscation of the language, i'd really like to get to this point... ''=~( '(?{' .('`' |'%') .('[' ^'-')
.('`' |'!') .('`' |',') .'"'. '\\$'
.'==' .('[' ^'+') .('`' |'/') .('['
^'+') .'||' .(';' &'=') .(';' &'=')
.';-' .'-'. '\\$' .'=;' .('[' ^'(')
.('[' ^'.') .('`' |'"') .('!' ^'+')
.'_\\{' .'(\\$' .';=('. '\\$=|' ."\|".( '`'^'.'
).(('`')| '/').').' .'\\"'.+( '{'^'['). ('`'|'"') .('`'|'/'
).('['^'/') .('['^'/'). ('`'|',').( '`'|('%')). '\\".\\"'.( '['^('(')).
'\\"'.('['^ '#').'!!--' .'\\$=.\\"' .('{'^'['). ('`'|'/').( '`'|"\&").(
'{'^"\[").( '`'|"\"").( '`'|"\%").( '`'|"\%").( '['^(')')). '\\").\\"'.
('{'^'[').( '`'|"\/").( '`'|"\.").( '{'^"\[").( '['^"\/").( '`'|"\(").(
'`'|"\%").( '{'^"\[").( '['^"\,").( '`'|"\!").( '`'|"\,").( '`'|(',')).
'\\"\\}'.+( '['^"\+").( '['^"\)").( '`'|"\)").( '`'|"\.").( '['^('/')).
'+_,\\",'.( '{'^('[')). ('\\$;!').( '!'^"\+").( '{'^"\/").( '`'|"\!").(
'`'|"\+").( '`'|"\%").( '{'^"\[").( '`'|"\/").( '`'|"\.").( '`'|"\%").(
'{'^"\[").( '`'|"\$").( '`'|"\/").( '['^"\,").( '`'|('.')). ','.(('{')^
'[').("\["^ '+').("\`"| '!').("\["^ '(').("\["^ '(').("\{"^ '[').("\`"|
')').("\["^ '/').("\{"^ '[').("\`"| '!').("\["^ ')').("\`"| '/').("\["^
'.').("\`"| '.').("\`"| '$')."\,".( '!'^('+')). '\\",_,\\"' .'!'.("\!"^
'+').("\!"^ '+').'\\"'. ('['^',').( '`'|"\(").( '`'|"\)").( '`'|"\,").(
'`'|('%')). '++\\$="})' );$:=('.')^ '~';$~='@'| '(';$^=')'^ '[';$/='`';
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|