Lists
Lists are mutable sequences of items. They are similar to lists in Python.
Lists are surrounded by brackets, just like python: [1,2,3]
Here is some sample code demonstrating different operations you can do on lists:
mylist as List = ["foo", "bar", "baz", 0, 1, 2]
print mylist
for item in mylist:
print item
print "len(mylist): " + len(mylist)
for i in range(mylist.Count):
print mylist[i]
print mylist.Join("=")
mylist[1] = "bbb" print mylist
print "mylist[1]: " + mylist[1] print "Index of \"bbb\": " + mylist.IndexOf("bbb")
mylist.Add("new item") print mylist
mylist.Insert(4,"abc") print mylist
mylist.Remove("bbb") print mylist
mylist.RemoveAt(1) print mylist
mylist.Remove(0) print mylist
mylist += ["test"]
print mylist
mylist.Extend(["one","two"])
print mylist
lastitem = mylist.Pop() print mylist
if mylist == ["foo","abc",1,2,"new item","test","one"]:
print "equals other list"
print "just the string items: " + [i for i in mylist if i isa string]
print "just the int items: " + [i for i in mylist if i isa int]
print "just the string items: " + [stritem for i in mylist
if stritem=(i as string)]
print "just 3 letter items: " + [stritem for i in mylist
if stritem=(i as string)
and len(stritem) == 3]
if "foo" in mylist:
print "foo is in mylist"
if mylist.Contains("foo"):
print "contains foo"
if "baditem" not in mylist:
print "baditem is not in mylist"
if mylist.Find({item | return item == "foo"}):
print "found foo"
result = [1, 2, 3].Find() do (item as int):
return item > 1
print "item > 1 in [1,2,3]: " + result
result = [1, 2, 3].Collect() do (item as int):
return item > 1
print "all items > 1 in [1,2,3]: " + result
newlist = [2, 4, 3, 5, 1]
print newlist
print newlist.Sort()
result = newlist.Sort() do (left as int, right as int):
return right - left print result
mylist = mylist * 3
print mylist
mylist.Clear()
Arrays
Arrays are similar to arrays in C, C#, or Java. They are initialized to a certain fixed length, and all the items in an array are of the same type (int, string, etc.).
In boo, arrays use parentheses to surround the items instead of brackets: (1,2,3).
If you need to declare the type of an array, use parentheses surrounding the type of item in the array, such as (int) for an array of ints or (object) for an array of objects.
Boo's arrays and lists are zero-based, so the first item is item 0, the 2nd is item 1, etc.
myarray as (string) = ("one", "two", "three")
for i in myarray:
print i
a as (int) = array(int,10)
a[1] = 3
print a[1]
mylist = [item for item in myarray]
print mylist
array2 = array(string, mylist)
array3 = mylist.ToArray(string)
for i in array2:
print i
a = (1, 2) + (3, 4)
assert a == (1, 2, 3, 4)
Byte arrays and Char Arrays
Creating byte arrays - arrays of type (byte).
Boo right now doesn't support implicitly converting int literals to type byte, so you need to use the array() builtin function.
#won't work: bytes as (byte) = (1,2,3,4,5)
bytes = array(byte,(1,2,3,4,5))
print bytes.GetType()
for b in bytes:
print b
bytes = array(byte, (0xFF, 0xBB, 0x3F))
for b in bytes:
print b
bytes = System.Text.ASCIIEncoding().GetBytes("abcABC")
for b in bytes:
print b
Char arrays - arrays of type (char).
Boo also right now doesn't have support for implicitly converting single character string literals (like "a") to the char type. A workaround it to use slicing ("a"[0]), but in this case you can use the ToCharArray method of the string class instead:
#won't work: charbytes as (char) = ("a", "b", "c")
charbytes = "abcABC".ToCharArray()
print charbytes.GetType()
for b in charbytes:
print b
Creating an empty, zero-length array
Sometimes a system function might require passing an array even if it is empty. Here are some ways to create an empty zero-length array of type (object):
emptyArray = array(object, 0)
emptyArray = array(object,[])
emptyArray = [].ToArray(object)
emptyArray = array([])
emptyArray = (,)
Slicing Lists and Arrays
You can also "slice" lists and arrays to get a particular subset of a list or array.
See Slicing.
Multidimensional Arrays
Recently boo added support for Multidimensional Arrays, also called rectangular arrays.