Page 197 -
P. 197
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
Cut the line of data
Average: 8.32
every time the split()
Board type: Fish method sees a semicolon.
Age: 21
Here's one possible solution.
line = “101;Johnny ‘wave-boy' Jones;USA;8.32;Fish;21"
Create an empty s = {}
hash called “s”.
(s[‘id'], s[‘name'], s[‘country'], s[‘average'], s[‘board'], s[‘age']) = line.split(“;")
Use multiple-
assignment to assign
the split data from print(“ID: " + s[‘id'])
“line” to “s”.
print(“Name: " + s[‘name'])
print(“Country: " + s[‘country'])
Display six nicely
formatted messages print(“Average: " + s[‘average'])
on screen.
print(“Board type: " + s[‘board'])
print(“Age: " + s[‘age'])
162 Chapter 5