When data gets complex

Hot on the heels of your success with the local surfing club, you've just been contacted by the Regional Surfing Association (RSA) and they want you to write a new program for them! RSA's offering a brand-new, state-of-the-art, epoxy resin surf board as payment... once the program's working to their satisfaction, of course.

You've been wanting to try out an epoxy board for ages. The trouble is, they're sooooo expensive and you're a poor surfer. The waves will have to wait (yet again). But, the thoughts of surfing an epoxy board... now that's worth waiting for.

So what does RSA's data look like?

Currently, RSA's data is stored in a text file using a proprietary data format. For each surfer, there's six pieces of data recorded on-- Here are each line in the file.

Here's what the data looks like:

101;Johnny 'wave-boy' Jones;USA;8.32;Fish;21 102;Juan Martino;Spain;9.01;Gun;36 103;Joseph 'smitty' Smyth;USA;8.85;Cruiser;18 104;Stacey O'Neill;Ireland;8.91;Malibu;22 105;Aideen 'board babe' Wu;Japan;8.65;Fish;24 106;Zack 'bonnie-lad' MacFadden;Scotland;7.82;Thruster;26 107;Aaron Valentino;Italy;8.98;Gun;19

the six p>etes da'ta'

Competition ID.

Name.

Country.

Average score.

Preferrec

board type.

Age.

K^daia is sirred in line, with a sernl/.oU separating each pie£e of data

RSA has tried importing this data into their favorite spreadsheet program, but that didn't really work for them. RSA wants a program that allows them to quickly find a surfers' data based on their Competition ID, then display the surfers' details like this:

UcM data item is »itely labeled-

ID: 101

Name: Johnny 'wave-boy' Jones

Country: USA

Average: 8.32

Board type: Fish Age: 21

Each data item is displayed on '1-ts own line, which makes t really easy to read (unlike the packed data file).

r ^harpen your pencil

Here's one surfer's data from the file, assigned to a variable called line:

line = "101;Johnny 'wave-boy' Jones;USA;8.32;Fish;21" Grab your pencil and write some code to process this line and display it on screen like this:

ID: 101

Name: Johnny 'wave-boy' Jones

Country: USA

Average: 8.32

Board type: Fish

Age: 21

Hints: If you pass a string parameter to the split () method, the data is cut where the string parameter occurs, as opposed to the cut occurring at a space character (which is split ()'s default behavior). Also, consider using multiple-assignment on the left side of the assignment operator when assigning more than one name-value pairing to a hash.

data display

Here's one surfer's data from the file, assigned to a variable called line:

line = "101;Johnny 'wave-boy' Jones;USA;8.32;Fish;21" You were to grab your pencil and write some code to process this line and display it on screen like this:

ID: 101

Name: Johnny 'wave-boy' Jones

Country: USA

Average: 8.32

Board type: Fish

Age: 21

Here's one possible solution-

Cut the line of data every time the splitO method sees a semicolon.

line = wl0l;Johnny 'wave-boy' Jones;WSA;032;Pish;2l"

(sC'id'J, sCname'], sfcountry'J, sfaverage'J, sfboard'J, sC'age'J) = line-split(Y')

Use multiple-assignment to assign the split data from pr

pr pr pr pr pr

Display six nicely__

formatted messages on screen.

nt("ID: " + s['id']) nt("Name: " + sCname'3) nt("Country: " + sfcountry'J) nt("Average: " + sfaverage'J) nt("Board type: " + sCboard']) nt("Age: " + sfage'J)

tqst drivq

As always, you need to enter this code into IDLE before you run it. Then, save your program and take it for a spin by pressing F5.

As always, you need to enter this code into IDLE before you run it. Then, save your program and take it for a spin by pressing F5.

Your code works for one line of data. But RSA wants to be able to display the data for any surfer, not just wave-boy's.

function to hash

Was this article helpful?

0 0

Post a comment