Online Book Reader

Home Category

HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [158]

By Root 1521 0
song out of your head. Sorry. Take a look at the following variation, which uses arrays and loops to simplify the code even more.

This code is just a little different from the antsParam program shown in the section of this chapter called “Passing Data to and from Functions.”

♦ It has an array called distractionList. This array is (despite the misleading name) a list of distractions. I made the first one (element zero) blank so that the verse numbers would line up properly.

♦ The verse() function looks up a distraction. Because distractions are now in an array, you can use the verseNum as an index to loop up a particular distraction. Compare this function to the verse() function in antsParam. This program can be found in the section “Passing data to and from Functions.” Although arrays require a little more planning than code structures, they can highly improve the readability of your code.

♦ The main program is in a loop. I step through each element of the distractionList array, printing the appropriate verse and chorus.

♦ The chorus() function remains unchanged. You don’t need to change chorus().

Working with Two-Dimension Arrays

Arrays are useful when working with lists of data. Sometimes, you encounter data that’s best imagined in a table. For example, what if you want to build a distance calculator that determines the distance between two cities? The original data might look like Table 4-1.

Table 4-1 Distance between Major Cities

0) Indianapolis

1) New York

2) Tokyo

3) London

0) Indianapolis

0

648

6476

4000

1) New York

648

0

6760

3470

2) Tokyo

6476

6760

0

5956

3) London

4000

3470

5956

0

Think about how you would use Table 4-1 to figure out a distance. If you wanted to travel from New York to London, for example, you’d pick the New York row and the London column and figure out where they intersect. The data in that cell is the distance (3,470 miles).

When you look up information in any kind of a table, you’re actually working with a two-dimensional data structure — a fancy term, but it just means table. If you want to look something up in a table, you need two indices, one to determine the row and another to determine the column.

If this concept is difficult to grasp, think of the old game Battleship. The playing field is a grid of squares. You announce I-5, meaning column I, row 5, and the opponent looks in that grid to discover that you’ve sunk his battleship. In programming, you typically use integers for both indices, but otherwise, it’s exactly the same as Battleship. Any time you have two-dimensional data, you access it with two indices.

Often, we call the indices row and column to help you think of the structure as a table. Sometimes, other names more clearly describe how the behavior works. Take a look at Figure 4-7, and you see that the distance.html program asks for two cities and returns a distance according to the data table.

Yep, you can have three,

Return Main Page Previous Page Next Page

®Online Book Reader