Table Data

< CS101

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 Example

Tables Are Very 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 code -- SimpleTable -- which will serve as a foundation for you to write table code. Run the code to see what it does.


Table Query Logic


The interesting thing to do is write some "query" logic where we pick out just the rows we care about.

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"

We can write an if-statement within the for-loop to test for certain rows. For each row, this code tests if the name == "Alice", pulling out and printing that one row:

Note that a single equal sign = does assignment to a variable and not comparison. It's a common mistake to type in one equal sign for a test, when you mean two equal signs. Unfortunately, JavaScript does not flag this error, so you have to look at your code and notice it. The regular less-than/greater-than type tests: < > <= >= work as have seen before.

Table Query Examples


Write in code above to solve these problems:

Solution code:

If logic inside the loop:

if (row.getField("name") == "Alice") {
  print(row);
}
// Change string to "Robert", "Bob", etc.

if (row.getField("rank") == 1) {
  print(row);
}

if (row.getField("rank") < 20) {
  print(row);
}

if (row.getField("gender") == "girl") {
  print(row);
}

s.startsWith("hi") s.endsWith("y")

For our purposes, strings support a s.startsWith("Ab") function, here testing if the string in the variable s starts with the "Ab" .. true or false. Likewise, there is s.endsWith("yz"), here testing if the string in variable s has "yz" at its very end. (Sadly, these two functions are not part of standard JavaScript; I made them work just for out code because they are so useful. These two functions are common in other computer languages.)

These tests work very well with the name strings pulled out of the baby data. Here we can look at all the names beginning with "Ab".


Solution code:

If logic inside the loop:

if (row.getField("name").startsWith("Ab")) {
  print(row);
}
// Change string to "A", "a", "Z", .. each in turn

if (row.getField("name").endsWith("z")) {
  print(row);
}

Boolean Logic: && || !

Boolean Logic Example 1

if (x.startsWith("A") && x.endsWith("z")) { ...

Boolean Logic Example 2

if (x.startsWith("A") || x.endsWith("z")) { ...

Boolean Logic Example 3

Here's the if-test for the query: popular names beginning with A (i.e. name starts with A and rank <= 50). Instead of x, now use the actual row.getField("name") to get the string out of the row.

In this case the test has been broken across multiple lines. You can break the test across multiple lines to avoid having so much code all on one line. Below the second and later lines are indented to line up with the first line for neatness (not required). Note that the if-statement still has the structure: if (test) { ..code.. } but with the test spanning multiple lines. CS101 will warn about common syntax errors for if-statements: omitting (test) parenthesis, curly braces, typing = instead of ==, typing & instead of &&, typing | instead of ||. In professional Javascript, things like a single | are allowed in rare cases, but for CS101 we'll figure that was just a typo.

  if (row.getField("name").startsWith("A") &&
      row.getField("rank") <= 50) { ...

Now add the constraint that they must be girl names:

  if (row.getField("name").startsWith("A") &&
      row.getField("rank") <= 50 &&
      row.getField("gender") == "girl") { ...

Many Query Code Examples

Here is the working code for the Popular A names test mentioned above.


Experiments to try:

Experiment solution code:

If logic inside the loop:

  // 1
  if (row.getField("name").startsWith("Ab") ||
      row.getField("name").startsWith("Ac")) {
    print(row);
  }

  // 2
  if (row.getField("name").startsWith("Ab") ||
      row.getField("name").startsWith("Ac") ||
      row.getField("name").startsWith("Al")) {
    print(row);
  }

  // 3
  if (row.getField("name").startsWith("O") &&
      row.getField("name").endsWith("a")) {
    print(row);
  }

  // 4
  if (row.getField("name").startsWith("O") &&
      row.getField("gender") == "girl") {
    print(row);
  }

  // 5
  if (row.getField("rank") <= 10 &&
      row.getField("gender") == "girl") {
    print(row);
  }

  // 6
  if (row.getField("name").endsWith("ia") &&
      row.getField("gender") == "boy") {
    print(row);
  }

  // 7
  if (row.getField("rank") <= 10 ||
      row.getField("gender") == "girl") {
    print(row);
  }

  // 8
  if (row.getField("name").endsWith("ia") &&
      row.getField("gender") == "boy") {
    print(row);
  }

  // 9
  if (row.getField("name").endsWith("io") &&
      row.getField("gender") == "girl") {
    print(row);
  }

  // 10
  if (row.getField("name").endsWith("o") &&
      row.getField("gender") == "boy" &&
      row.getField("rank") >= 900) {
    print(row);
  }

Not !

Not Code Examples


Experiments to try:

Experiment solution code:

If logic inside the loop:

  // 1
  if (!row.getField("name").startsWith("A")) {
    print(row);
  }

  // 2
  if (!row.getField("name").startsWith("A") &&
      row.getField("gender") == "girl") {
    print(row);
  }

  // 3
  if (row.getField("name").startsWith("A") &&
      !row.getField("name").endsWith("y")) {
    print(row);
  }

  // 4
  if (row.getField("name").startsWith("A") &&
      row.getField("name").endsWith("y") &&
      row.getField("name") != "Abbey") {
    print(row);
  }