[INTRO] Some basics about variables and lists!

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.

image

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.

image

image

If you did not set the name of a variable properly, an error will occur.

image

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.

image

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.

image

Unlike global variables, you can add global variables, parameters or procedures to local variables. However, it does not support other local variables.

image

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.

:heavy_check_mark: Contain @, $, _, ?

:heavy_check_mark: Contain English alphabets

:heavy_check_mark: Contain numbers that are not at the start of variables

:heavy_check_mark: Contain Chinese, Greek, Arabic characters, Ü, ß, Ç

:heavy_check_mark: Contain Unicode characters or emojis.

:heavy_check_mark: Start with zero-whitespace Unicode character, then numbers

:x: Contain !, #, ^, &, *, (, ), =, +, /, , , . , :.

:x: 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.

lists_create_with

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.

image

lists_create_with (1)

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).

blocks (5)

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.

blocks

The is in list? block checks whether the 'thing' is stored in the 'list'.

blocks (1)

The length of list block returns a number, indicating the length of the list, in other words, how many items the list consists.

blocks (2)

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.

image

You can use the remove item from list block to remove a list item.

blocks (6)

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.

blocks (3)

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.

blocks (5)

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

:+1: Rate my tutorial! :-1:

  • Good tutorial!
  • Bad tutorial.
0 voters

Kindly :email: PM me if you have any questions! Also, if you like my tutorial, please :heart: 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

:speech_balloon: Message :earth_africa: Website :e-mail: E-mail

8 Likes

good guide!

1 Like

Also mention that only on global variables we can use Do it to degug our blocks, not on local

4 Likes

Now added to my website - see main topic.

1 Like

I did not see any recommendations for how to tie your variables to the app objects by their names, using name parts like

  • of
  • index
  • list
    and nouns like
  • phoneNumber
  • Name
  • emergencyContact
  • date
  • location
1 Like

This is very confusing to me. I assume that you want to say:
the value is NOT set to the original value.

I think that referring to AI classic is only confusing to most people. If they are new to AI then probably they never heard about AI classic.

What do you mean by this? Again, I think that you say the opposite of what you want to say.
I think it needs to read something like: you can define and use local variables in, or parameters to a procedure. The value of these is only known within this procedure.

I have included a screenshot for that.

You see, if you attempt to add 'name' to the soclet for 'x3', it will result in an error. However, if you add 'global demo' to 'x', no errors will occur, unlike global variables. If you add 'component' as a socket into 'name' as a list, no errors will occur.

Then should I delete it?

No, that is exactly what I want to say.

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.

A more positive example might be more illuminating

image

2 Likes

It's good to say what it depends to. New users might not know.
But still a good guide for beginners!

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.

I meant change the OP. I already knew that

Yes, but that is not what you said:

For a TinyDB you take about tags, not variables.

1 Like

Yes, you should do that.

Incredible

:clap: :wink:

1 Like