Sampi

Home Older Versions Documentation Changelog Learn


Lists and Dictionaries

If variables are like a box, then lists are like a box split up into little compartments. You can tell it which compartment you want to store a value in, and which compartment you want to get a value from. The compartments are numbered 1, 2, 3, 4, and so on. Sampi lists use 1 based indexing. You can create a list with square brackets [].


set x to ["a", "b", "c"]
say(x[1])
        

To get how long a list is, use length().


say(length([1, 2, 3]))
        

length() works on strings, and dictionaries as well. A dictionary is, well, a dictionary! It is like a list, but instead of using 1, 2, 3, 4, and so on, it uses any label you want! You can make a dictionary using curly braces {}.


set x to {"a":1, "b":2}
say(x["a"])
        

You can edit the contents of lists and dictionaries using somthing called a method. A method is a special command that you can run on a specific value.


set x to ["a", "b", "c"]
        

MethodExampleResult
add()x.add("d")["a", "b", "c", "d"]
remove()x.remove("b")["a", "c"]
delete()x.delete(3)["a", "b"]
get_index()x.get_index("c")3
insert()x.insert(1, "hello")["a", "hello", "b", "c"]
clear()x.clear()[]
join()x.join(", ")"a, b, c"


set x to {"a": 1, "b": 2, "c": 3}
        

MethodExampleResult
add()x.add("d", 4){"a": 1, "b": 2, "c": 3, "d": 4}
remove()x.remove("b"){"a": 1, "c": 3}
clear()x.clear(){}

Somtimes, you want to loop through the contents of a list. This can be useful for saying every item or to change every item, like to double them. Just make a repeat loop, but use a list instead of a number.


letters = ["a", "b", "c", "d"]

repeat letters as letter:
        say(letter)
end
        

Previous