A few weeks ago I introduced you to three scripting languages -- Python, Perl, and Lua. We talked about their REPLs and looked at the simple expression and output syntax along with how to exit the REPLs.

At the end of the article I encouraged you to play around with some simple expressions and the print statement and see what you could achieve with your chosen language(s).

This time, I'd like to introduce you to some simple data structures available in the three scripting langauges and some simple ways to interact with them. We've already seen numbers and strings of characters, but most languages have at least one or two more basic data structures available. Almost everything else in the language will be built out of the basic data structures, so mastering them is both important and a long-winded process.

Python

In Python, there are a number of core data structures which may have syntactic support. All of them are classes, but built-in to the interpreter and language are the numbers and strings that we've encountered already, along with lists, dicts, tuples, and sets. (There are a few more such as booleans, but I trust you can look those up for yourselves.)

>>> 12 # Number
12
>>> "string"
"string"
>>> ["list","of","strings"]
['list', 'of', 'strings']
>>> {"dict":"of","things":14}
{'things': 14, 'dict': 'of'}
>>> set([1,2,3,4,5])
set([1, 2, 3, 4, 5])
>>> ("two","tuple)
('two', 'tuple')

Lists, dictionaries, sets, tuples, etc are all objects which have the requisite class (list, dict, set, tuple) and you can define your own classes and construct instances of them as so:

>>> class Foo:
...     def demo(self):
...         print "Hello World"
...
>>> Foo().demo()
Hello World
>>>

You can investigate what an object can do in python in a few ways. The most useful way from the point of view of the REPL is simply to ask for help:

>>> help([])
......paged documentation about the list class here......

Perl

Perl has a smaller set of fundamental types. Perl has numbers, strings, lists, dictionaries (which Perl calls hashes), file handles, and a few other bits and bobs. Perl is slightly different from Python in how it presents those types to the developer. Perl has what are called type sigils which, for the most part, can be used to control the kind of value you're talking about when you use variables in expressions.

$_ my $foo = 1;  # The $ indicates a scalar (single) value
1$_
$_ my @bar = (1,2,3); # The @ indicates a list of values
$VAR1 = 1;
$VAR2 = 2;
$VAR3 = 3;
$_ my %baz = ("foo" => 1, "bar" => 2); # The % indicates a hash
$VAR1 = 'foo';
$VAR2 = 1;
$VAR3 = 'bar';
$VAR4 = 2;
$_

Notice how the style of re.pl output for lists and hashes are similar? That is because the initialiser for a hash is simply a list of the form (key1,value1,key2,value2,...) and the => operator in Perl is just a fancy way of spelling the comma (,).

I mentioned file handles before, but I'm sure you can go and investigate for yourself if you're fancying Perl. Perl is exceedingly widely used and very commonly supported on Linux-based operating systems; so it is a very good idea to get at least passingly familiar with how to read it.

Perl's classes are built out of the hash data type and involve a process which Perl calls blessing. Have fun with that.

Lua

Lua has a number of fundamental data types we've already seen, and a few which we've not, but along with its strings, numbers, booleans, functions, etc. Lua has a single data type which joins together the concept of a list and a dict. Lua has a type which it calls table.

> = {foo = "bar"}
table: 0xbab440
> = {1, 2, 3}
table: 0xbab800

Sadly Lua's REPL doesn't expand tables for us, so we can't see inside them easily, but I'm sure you can go and look up how to look inside them if you try.

Lua's classes are built out of tables, which are a very powerful data type once you add in a little more Lua magic called metatables. Enjoy looking those up.

Challenge

This time, I'd like you to take on your favourite of the three languages we've been discussing (or perhaps all three of them if you're feeling adventurous) and have a play with the basic data types. Next time we'll talk about how you can break up your program into reusable chunks, typically called functions, procedures, or methods.