Lists on Lists on Lists, Programming Syntax Breakdown

Posted Nov 11, 2014

In this back to basics entry we break down the different ways the most popular web programming languages list syntaxes. The other goal is to reinforce the fact that programming languages are fairly similar.

One of the first mental blocks beginner programmers confront is picking the right language to learn, also a great way to procrastinate from actually learning to code. Our advice? JUST FUCKING PICK ONE!

Recognizing that programming languages operate with a similar set of principles is what helps you the most. Think about picking up girls, after awhile you begin to notice a pattern. Mastery of these patterns let's you move more easily from language to language, only worrying about syntax changes. In this blog we'll break down list syntaxes in different languages. Lists are a huge part of programming, since most software is just manipulating information in a repetitive fashion. And how do you organize that information? Lists, on lists, on lists, or lists within lists if you're into Inception!

In programming lists come in two main varieties, some languages like Python have even more, but we'd like to keep this blog a reasonable length. The first type is a plain list, the simplest data structure.

Lists

Lists are the most basic groupings of info.  The most important characteristic of a list is that the items are in a specific order.

Hash (short for Hash Table) / Associative Arrays / Dictionaries

The second type of list is a little more advanced, hence why it falls into the category of Advanced Data Structure.  These creates a group of information where each element contains a key and a value. This allows for quick access to any piece of information in the array, by using the unique hashable key.  Hashable means that we can compute a numeric value from a key which is used as an index in the array.

Syntax Breakdowns!

PHP

PHP confuses people because their 'array' isn't like other arrays.  In fact they're not really arrays, they're ordered hash tables.  The syntax is very similar to other languages though.

List (which is always an array in PHP)

 $big_lebowski = array (
    "the dude",
    "walter",
    "donny"
) 

//Keyed Array
$big_lebowskey = array (
    "the dude" => "Jeffry Lebowski",
    "walter" => "Walter Sobchak",
    "donny" => "Theodore Donald Kerabatsos"
)

PHP 5.4 Arrays

As of PHP 5.4 a new array shorthand has been introduced which is very similar to Javascript's literal array notation.

// as of PHP 5.4
$big_lebowski = [
    "the dude" => "Jeffrey Lebowski",
    "walter" => "Walter Sobchak",
    "donny" => "Theodore Donald Kerabatsos"
];

Python

We'll discuss Python first since they get a little complicated with their lists, because in Python there are many ways to organize information.  We'll concentrate on the main two, lists and dictionaries.  Or list and  dict.

List

A python list just contains values and is created with the square bracket [ ] syntax.:

biglebowski = [
    "the dude",
    "walter",
    "donny"
]

A list is also known as mutable, or an un-hashable object since the values of a list can be changed.  That differentiates it from dict. Dict key values are immutable, meaning un-changeable.

Dictionary

Dict associates each key with a value, so would be similar to associative arrays and hash tables in other languages.

 big_lebowski = {
    "the dude": "Jeffrey Lebowski",
    "walter": "Walter Sobchak",
    "donny": "Theodore Donald Kerabatsos"
}

These to are only the beginning with Python! There's also collections like sets, tuples, and named tuples. We could do a whole entry just on Python collections, but for brevity's sake we'll stop at two in this entry.

Ruby

Like python, ruby uses two types of data structures for organizing information, the list and the hash. Hash is the Ruby equivalent of a Python dictionary.

List

Once again, a list is created with the square bracket sequence, [ ].  Notice a pattern here?

 biglebowski = [
    "the dude",
    "walter",
    "donny",
]

Hash

Ruby refers to their key: value containers as hashes, much to the chagrin of the theoretical computer science police (a pseudo organization of overzealous neck beards on stackoverflow).

 big_lebowski = {
    "the dude": "Jeffry Lebowski",
    "walter": "Walter Sobchak",
    "donny": "Theodore Donald Kerabatsos"
}

Javascript

List or Array Literal

Javascript uses the square bracket syntax like many other languages to create a list of values.

 var bigLebowski = [
    "the dude",
    "walter",
    "donny",
]

Javascript arrays also can be created with the array() syntax, but most modern javascript is written with the square bracket syntax.

Something Like an Associative Array

Javascript doesn't really have an array notation, so to make an associative array you create a javascript object with the squiggly braces notation, { }.

 var bigLebowski = {
    "the dude": "Jeffry Lebowski",
    "walter": "Walter Sobchak",
    "donny": "Theodore Donald Kerabatsos"
}