A. Introduction
This is a guide written to introduce variables and lists to new beginners. I, of course, agree that the documentation is also another way, and the purpose I wrote this tutorial is to make life easier for beginners and newbies. Other sources might not be a problem with advanced users, but it might confuse beginners.
Click here and here for more info.
B. Variables
Variables are pieces of data you can temporarily store. For example, I might want a variable to indicate the current score, or a variable to indicate a random number.
These are the differences between variables and TinyDB.
Variables:
You declare a variable x and set its value as 0 at the start of the program. The user clicks a button, and the variable is set to 1. The user exits the application and opens again. The variable x was back as 0, not 1.
TinyDB:
You store a tag x and set its value as 0 at the start of the program. The user clicks a button and the value of the tag is set to 1. The user exits the application and opens again. The tag x is still 1.
There are global and local variables. Global variables are variables that can be accessed any where in the screen, but local variables can only be accessed in a specific part of the program. Parameters are a type of local variables.
B1. Global variables
To create a global variable, use this.
You can change the name of the variable by clicking on 'name'. The socket of the variable is the value that the program will first set the variable. If you did not plug in anything, the default value is 0.
Global variables support components, texts, numbers, colors, lists, booleans, dictionaries and helper blocks, but do not support other variables or procedures. This is because another variable or procedure must not be in a definition.
If you hover over a variable, you will see it has a get and set block to get and set the value of the variable. You can drag them out from there, or drag one out from the Built-in > Variables drawer.
If you did not set the name of a variable properly, an error will occur.
B2. Local variables
To create a local variable, use one of these, depending on your needs. If you are running a procedure that returns something, you can use the second one. If you are running a procedure that does something, use the first one.
Local variables are variables that can only be accessed among the mutators. To create another local variable, click on the blue settings icon and drag the 'name x' block into the mutator.
Unlike global variables, you can add global variables, parameters or procedures to local variables. However, it does not support other local variables.
You can only debug global variable blocks using Do It. To debug local variables, set a global variable value to the local variable value, then Do-It.
B3. What type of text does variable names accept?
From this post.
Contain @, $, _, ?
Contain English alphabets
Contain numbers that are not at the start of variables
Contain Chinese, Greek, Arabic characters, Ü, ß, Ç
Contain Unicode characters or emojis.
Start with zero-whitespace Unicode character, then numbers
Contain !, #, ^, &, *, (, ), =, +, /, , , . , :.
Start with a number
Even though you cannot make a variable start with a number, you can make it look like it does. The same topic in the link above provides a website that allows you to copy a Unicode white space that is invisible.
C. Lists
Lists store a series of data. A string or a number provides a single piece of data, while lists can have dozens of data provided. For example, I might want a list of available types of coffee, or a list of players.
The create empty list block creates an empty list. It has 0 elements, therefore has 0 indexes.
To understand an index, let's make up a list - 'App', 'Inventor' and 'iOS'.
According to the above example, I would like to introduce some terms.
Length - the length of the list is 3 (it has 3 items in the list)
Index - the index of the item 'App' in the list is 1, because it is the first item in the list. Index 2 belongs with Inventor and iOS is 3.
To add a socket into the list block, click on the blue settings icon and drag the 'item' block into the mutator.
The make a list block provides sockets to plug in. The blocks you plug in will then be stored as a list.
This is how you create a list and temporarily place it.
Lists support strings, booleans, numbers, colors, new lists, dictionaries, components and helper blocks. Lists also support variables and procedures, only if the blocks are not in a definition (i.e. variables).
You can add items to a list by using the add items to list blocks. By clicking on the blue settings icon, you can add new items.
The is in list? block checks whether the 'thing' is stored in the 'list'.
The length of list block returns a number, indicating the length of the list, in other words, how many items the list consists.
The pick a random item block randomly selects an item in the list. If you use this block for an empty list, the app will report an error.
The index in list block returns the index of the given item in the list, while the select list item block returns an item in the list prior to the given index.
You can use the remove item from list block to remove a list item.
The list from csv row block converts a text string to a list. Turning 1,2,3 to a list will result in a list having 1, 2 and 3 as its list items.
On the opposite, you can turn a list back into a string. The join items using separator block returns a text string that converts the list into text, with all items separated in the 'separator'.
With the append to list block, list2 will be combined with list1. All of the items in list2 will appear at the back of list1.
D. Why lists and variables?
Lists and variables are intended to make life easier for everyone. Without lists and variables, we will have to frequently store and delete data in databases, which confuses people and often times end up with bugs. Guess what, without lists and variables, you are unlikely to make an advanced application, probably just a toggle game.
E. Helpful Links
http://ai2.appinventor.mit.edu/reference/blocks/lists.html
http://ai2.appinventor.mit.edu/reference/blocks/variables.html
Video tutorial:
F. Other Affairs
Rate my tutorial!
- Good tutorial!
- Bad tutorial.
Kindly PM me if you have any questions! Also, if you like my tutorial, please like it! It takes some effort for me to make it...
Votes and likes tell me the general user feedback of my tutorial. If you read this tutorial, please take 20 seconds to drop by and give a vote / like!
If you have any features that you want to add to this tutorial, PM me.
Happy inventing!
Gordon Lu