Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
sorting program for golf scores

Posted by BJ (BJ), 6 April 2005
Which sorting programs might work for this...i have not much experience in sorters or arrays as of yet...thanks,,...

Posted by admin (Graham Ellis), 6 April 2005
Almost every language has its own sorting routines which can be tailored to suit the data, and typically these sort routines are built into the language or system rather that being separate programs.  Exactly what and how they work depends on the data involved.

Here's an example in Perl:

Code:
# Sorting golf scores

# Read the scores

@scores = <DATA>;
print "Original scores:\n",@scores,"\n\n";

# Sort and print the leaderboard
@leaderboard = sort byscore @scores;
print "Leader Board:\n",@leaderboard,"\n\n";

sub byscore {
       # Calculate fields needed in sorting 2 records
       @fa = split(/\s+/,$a);
       @fb = split(/\s+/,$b);
       $scorea = $fa[-1]+$fa[-2]+$fa[-3];
       $scoreb = $fb[-1]+$fb[-2]+$fb[-3];
       # Compare those records
       $scorea <=> $scoreb or $fa[1] cmp $fb[1];
       }

__END__
Arjun Atwal (Ind) 77 67 64
Arron Oberholser 72 68 69
Brian Bateman 72 68 71
Dan Forsman 73 72 67
Dean Wilson 72 70 69
Frank Lickliter II 71 70 68
Hunter Haas 75 68 67
Joey Snyder III 71 69 71
Jose-Maria Olazabal (Spa) 70 69 69
Lucas Glover 74 67 69
Omar Uresti 71 74 67
Phil Mickelson 74 65 69
Retief Goosen (Rsa) 72 69 71
Ryuji Imada (Jpn) 70 71 70


Like many othe languages, Perl's built in sort routine can take an extra parameter which is the name of a piece of code that tells two records apart by returning a negative, zero or positive response when comparing them to see which is "less" than the other.

You'll find links to further (Perl) source code examples at http://www.wellho.net/resources/P211.html for Perl, at http://www.wellho.net/resources/Y111.html for Python and at http://www.wellho.net/resources/H999.html for PHP.

Here's the result of running the program (above) to test it:

Code:
earth-wind-and-fire:~/apr05 grahamellis$ perl gsc
Original scores:
Arjun Atwal (Ind) 77 67 64
Arron Oberholser 72 68 69
Brian Bateman 72 68 71
Dan Forsman 73 72 67
Dean Wilson 72 70 69
Frank Lickliter II 71 70 68
Hunter Haas 75 68 67
Joey Snyder III 71 69 71
Jose-Maria Olazabal (Spa) 70 69 69
Lucas Glover 74 67 69
Omar Uresti 71 74 67
Phil Mickelson 74 65 69
Retief Goosen (Rsa) 72 69 71
Ryuji Imada (Jpn) 70 71 70


Leader Board:
Arjun Atwal (Ind) 77 67 64
Phil Mickelson 74 65 69
Jose-Maria Olazabal (Spa) 70 69 69
Frank Lickliter II 71 70 68
Arron Oberholser 72 68 69
Lucas Glover 74 67 69
Hunter Haas 75 68 67
Brian Bateman 72 68 71
Ryuji Imada (Jpn) 70 71 70
Joey Snyder III 71 69 71
Dean Wilson 72 70 69
Dan Forsman 73 72 67
Retief Goosen (Rsa) 72 69 71
Omar Uresti 71 74 67


earth-wind-and-fire:~/apr05 grahamellis$


Posted by BJ (BJ), 8 April 2005
Thank you..that was quite over my head...lol...but at least i have a general idea of how it fits into the overall picture. ...One more question....in order for files etc. to be entered into arrays...do they have to be sorted first?...forgive my ignorance but im only in my first few weeks of programming and logic. we have just begun the study of arrays and sorting methods.....Thanks  

Posted by admin (Graham Ellis), 8 April 2005
Information can be put into arrays (or lists) in any order ... typically, you'll read a file or a result set from a user and put the information into the array in the order you read it.  You then sort it from the array or list and (depending on the language) the results will either be put back into the same array or returned to you as a new array.

Posted by BJ (BJ), 8 April 2005
Interesting..I had it backwards...Thanks



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho