As you saw in the previous lesson, you can add and replace elements in lists. You have to be careful, however, when putting elements of a different type in one list. Let's add a number to your previous list containing names and then fetch this new element and use it in mathematical operation:
♥list = Adam❚Eva❚John❚Mary
♥list⟦⟧ = 200
dialog ♥list
♥test = ♥list⟦5⟧+10
dialog ♥test
You added 200
at the end of the ♥list
as its fifth element, then you fetched this element to add it to 10
and assign the result to the ♥test
variable. You expected the second dialog would show 210
, but you saw 20010
instead. Why?
Variables can be of different types, for example: integer (integer numbers with no decimal point), float (numbers with decimal point), text (strings of alphanumerical characters), bool (boolean true or false). When you create a list, the type of its first element defines the whole list's type.
In the example above, the first element of the list is text. Therefore, when you add any number to this list, it will be automatically converted by the G1ANT.Robot to a text type. This means 200
you added to ♥list
is treated as a string of characters, not actual number, so when you wanted to add 10
to it, the robot simply merged these two strings of text, producing 20010
instead of expected 210
.
In the next lesson you will learn how to use variables as parameters for operations on lists.