Table-1 Data
As another example of how data is stored and manipulated in the computer, we'll look at "table data" -- a common a way to organize strings, numbers, dates in rectangular table structure. In particular, we'll start with data from the social security administration baby name site.
Social Security Baby Name Table
Tables Are Extremely Common
Much of the information stored on computers uses this table structure. One "thing" we want to store -- a baby name, someone's contact info, a craigslist advertisement -- is one row. The number of fields that make up a row is fairly small -- essentially the fixed categories of information we think up for that sort of thing. For example one craigslist advertisement (stored in one row) has a few fields: a short description, a long description, a price, a seller, ... plus a few more fields.
The number of fields is small, but the number of rows can be quite large -- thousands or millions. When someone talks about a "database" on the computer, that builds on this basic idea of a table. Also storing data in a spreadsheet typically uses exactly this table structure.
Table Code
We'll start with some CS101 code -- SimpleTable -- which will serve as a foundation for you to write table code. Run the code to see what it does.
Extract Field From Row - getField()
Table Query 1 - Rank
Table Query 2 - Name
The above code loops over all the rows, and the if-statement prints just the rows where the test is true, here testing if the rank field is equal to 6, but really the if-statement could test anything about the row.
The row object has a row.getField("field-name")
function which returns the data for one field out of the row. Each field has a name -- one of "name" "rank" "gender" "year" in this case -- and the string name of the field is passed in to getField() to indicate which field we want, e.g. row.getField("rank")
to retrieve the rank out of that row.
You can test if two values are equal in JavaScript with two equal signs joined like this: ==
. Using ==, the code to test if the name field is "Alice" is row.getField("name") == "Alice"
Note that a single equal sign =
does variable assignment and not comparison. It's a common mistake to type in one equal sign for a test, when you mean two equal signs. For this class, the Run button will detect an accidental use of a single = in an if-test and give an error message. The regular less-than/greater-than type tests: < > <= >= work as have seen before.
Table Query Examples
Example queries, and some you-try-it
- Baby table fields: name, rank, gender, year
- name field is "Robert", "Bob", "Abby", "Abigail" (try each in turn, yes nobody names their child "Bob" .. apparently always using Robert or Bobby)
- rank field is 1
- rank field is < 10
- rank field is <= 10
- rank field is > 990
- gender field is "girl"
- You try it:
- rank field is less than 15
- gender field is "boy"
- In all cases:
The loop goes through all 2000 rows and evaluates the if-test for each, printing that row only if the test is true.
Solution code: