Database |> Language |> Types |> Keywords |> 

ASSIGN

Contents

Definition

The ASSIGN keyword is what variable and function assignments in pipe operator syntax are implicitly translated to. The below expression:

hn <| https://news.ycombinator.com |

Is equivalent to the following expression:

ASSIGN hn https://news.ycombinator.com

Another way to view the ASSIGN keyword is like the alias command in shell.

CSS Selectors

One reason to use assignments is for assigning variables which offer a readable shorthand for referring to locating elements on a page.

readable_name <| div.crazy.class[some="attribute-value"] |

Output from a function

Another reason for leveraging assignments is for transforming the output of a function into a value that can used in subsequent instructions via list comprehension. To see a more complete example, go here.

some_value <| func_that_results_in_data |> {index} |

When list comprehension is used in assignment, it can be used for either retrieving a certain index or a range of values from each nested list.

nested_sublists <| func_that_results_in_data |> {start:end} |

In the range-based scenario, it follows the same rules as python where you can specify a third step value to indicate that you’d like to increment the counter from which you’re gathering values by a value other than one.

nested_skipping_sublists <| func_that_results_in_data |> {start:end:step} |

Example

In order to get the post titles and links from the front page of hacker news, we’ll need the following values in addition to the exact URL of the page we’re interested in:

  • The repeating container that wraps each post block
  • The selector for the title of the post
  • The href attribute of the link associated with the post

In our favor, the last two values belong to the same anchor tag allowing us to succinctly assign the href attribute in the below block of assignments:

-- The URL we're interested in
hn <| https://news.ycombinator.com |

-- The repeating container that wraps each post
container <| span.titleline |

-- The element containing the title of a post
post <| a |

-- The element's href attribute for its link
post_link <| a@href |

The benefit of this is you then get a resulting query statement at the end that’s more readable than what you’d otherwise construct in a different framework.

FROM hn
|> GROUP BY container
|> SELECT post, post_link

Putting it all together gives us:

-- The URL we're interested in
hn <| https://news.ycombinator.com |

-- The repeating container that wraps each post
container <| span.titleline |

-- The element containing the title of a post
post <| a |

-- The element's href attribute for its link
post_link <| a@href |

FROM hn
|> GROUP BY container
|> SELECT post, post_link

Related: