#!/usr/bin/php "Germany", "Alain Prost" => "France", "Ayrton Senna" => "Brazil", "Rubens Barrichello" => "Brazil", "David Coulthard" => "Great Britain", "Nélson Piquet" => "Brazil", "Nigel Mansell" => "Great Britain", "Mika Häkkinen" => "Finland" , "Niki Lauda" => "Austria", "Gerhard Berger" => "Austria", "Fernando Alonso" => "Spain", "Damon Hill" => "Great Britain", "Jackie Stewart" => "Great Britain", "Kimi Räikkonen" => "Finland", "Ralf Schumacher" => "Germany", "Juan Pablo Montoya" => "Columbia", "Carlos Reutemann" => "Argentina", "Emerson Fittipaldi" => "Brazil", "Riccardo Patrese" => "Italy", "Graham Hill" => "Great Britain", "Jim Clark" => "Great Britain", "Jack Brabham" => "Australia", "Denny Hulme" => "New Zealand", "Jody Scheckter" => "South Africa", "Giancarlo Fisichella" => "Italy", "Juan Manuel Fangio" => "Argentina", "Jean Alesi" => "France", "Jacques Villeneuve" => "Canada", "Jacques Laffite" => "France", "Jenson Button" => "Great Britain", "Clay Regazzoni" => "Switzerland", "Ronnie Peterson" => "Sweden", "Alan Jones" => "Australia", "Eddie Irvine" => "Great Britain", "Bruce McLaren" => "New Zealand", "Michele Alboreto" => "Italy", "Stirling Moss" => "Great Britain", "Jacky Ickx" => "Belguim", "René Arnoux" => "France", "John Surtees" => "Great Britain", "Mario Andretti" => "USA", "James Hunt" => "Great Britain", "Heinz-Harald Frentzen" => "Germany", "Jarno Trulli" => "Italy", "John Watson" => "Great Britain", "Keke Rosberg" => "Finland", "Patrick Depailler" => "France", "Dan Gurney" => "USA", "Thierry Boutsen" => "Belguim", "Elio de Angelis" => "Italy", "Nino Farina" => "Italy", "Mike Hawthorn" => "Great Britain", "Alberto Ascari" => "Italy", "Jochen Rindt" => "Austria", "Felipe Massa" => "Brazil", "Patrick Tambay" => "France", "Richie Ginther" => "USA", "Gilles Villeneuve" => "Canada", "Didier Pironi" => "France", "Martin Brundle" => "Great Britain", "Johnny Herbert" => "Great Britain", "Phil Hill" => "USA", "François Cévert" => "France", "Stefan Johansson" => "Sweden", "Chris Amon" => "New Zealand", "Nick Heidfeld" => "Germany", "Jean Pierre Beltoise" => "France", "Olivier Panis" => "France", "Tony Brooks" => "Great Britain", "Maurice Trintignant" => "France", "Oscar Gonzalez" => "Mexico", "Pedro Rodriguez" => "Mexico", "Jochen Mass" => "Germany", "Derek Warwick" => "Great Britain", "Eddie Cheever" => "USA", "Mark Webber" => "Australia", "Jo Siffert" => "Switzerland", "Alessandro Nannini" => "Italy", "Peter Revson" => "USA", "Andrea de Cesaris" => "Italy", "Lorenzo Bandini" => "Italy", "Carlos Pace" => "Brazil", "Wolfgang von Trips" => "Germany", "Jean Behra" => "France" ); // the analysis code $datar = explode( "\n", $data ); // the formatted version of $datar $formattedr = array(); // the list of nations (to be populated later) $nations = array(); foreach( $datar as $l ) { $l = explode( ",", $l ); $formattedr[] = array( "name" => $l[0]." ".$l[1], "points" => $l[2], "wins" => $l[3], "poles" => $l[4], "fl" => $l[5], "pods" => $l[6] ); } // populate all of the statistics for each nation foreach( $formattedr as $driver ) { $this_nat = $drivers[$driver['name']]; // this drivers nationality // start a new nation if it hasn't been done already if( !isset( $nations[$this_nat] ) ) { //print( $this_nat." & ".$driver['name'] ."\n" ); $nations[$this_nat] = array( "nation" => $this_nat, "points" => 0, "wins" => 0, "poles" => 0, "fl" => 0, "pods" => 0 ); } // add this drivers statistics $nations[$this_nat]['points'] += $driver['points']; $nations[$this_nat]['wins'] += $driver['wins']; $nations[$this_nat]['poles'] += $driver['poles']; $nations[$this_nat]['fl'] += $driver['fl']; $nations[$this_nat]['pods'] += $driver['pods']; } // we need a list of columns to really sort // Obtain a list of columns foreach ($nations as $key => $row) { $nation[$key] = $row['nation']; $points[$key] = $row['points']; $wins[$key] = $row['wins']; $poles[$key] = $row['poles']; $fl[$key] = $row['fl']; $pods[$key] = $row['pods']; } // do the actual sorting switch( $argv[1] ) { case "points": if( array_multisort ( $points, SORT_DESC, $nations ) ) { foreach( $nations as $nation ) print( $nation['nation']." ".$nation['points']."\n" ); } else { die( "Couldn't sort array :-(\n" ); } break; case "wins": if( array_multisort ( $wins, SORT_DESC, $nations ) ) { foreach( $nations as $nation ) print( $nation['nation']." ".$nation['wins']."\n" ); } else { die( "Couldn't sort array :-(\n" ); } break; case "poles": if( array_multisort ( $poles, SORT_DESC, $nations ) ) { foreach( $nations as $nation ) print( $nation['nation']." ".$nation['poles']."\n" ); } else { die( "Couldn't sort array :-(\n" ); } break; case "fl": if( array_multisort ( $fl, SORT_DESC, $nations ) ) { foreach( $nations as $nation ) print( $nation['nation']." ".$nation['fl']."\n" ); } else { die( "Couldn't sort array :-(\n" ); } break; case "pods": if( array_multisort ( $pods, SORT_DESC, $nations ) ) { foreach( $nations as $nation ) print( $nation['nation']." ".$nation['pods']."\n" ); } else { die( "Couldn't sort array :-(\n" ); } break; default: die( "Invalid choice.\n"); } ?>