Lesson 1.6 - Using Arrays
- Description
- You can assign many variables with the same name, and distinguish between them by using index values. For example, you can have three different variables named
a[1], a[2], and a[3]. A group of variables like this is called an array.There are two ways you can set the values of array variables: - You can set the value of one variable individually with a statement such as
<webSet #a[2]# = 7> - You can set multiple values at once with a statement like
<webSet #a[]# = ",0,1,,3">
In the second statement, the first character of the string is the delimiter which separates different values. You can use any character as the delimiter. That statement will set values for a[0], a[1], ..., a[n] depending on how many values are contained in the string. - You can also count the number of items inside an array like
#ArrayCount(a[])#.
- Syntax
<webSet #variable[index]# = expression> <webSet #variable[]# = delim & value0 & delim & value1 & delim & ... & delim & valuen> <webPrint> ...#variable[index]#... </webPrint> #ArrayCount(Name of the array)#
- Example
<webSet #a[0]# = 3> | <webSet #b[50]# = "Web+"> | <webSet #c[]# = ",0,1,2,,,,,7"> | sets c[0]=0, c[1]=1, c[2]=2, c[7]=7 | <webSet #city[]# = "/Minneapolis, MN/Boston, Mass./San Francisco, CA"> | city[0]="Minneapolis, MN",
city[1]="Boston, Mass.",
city[2]="San Francisco, CA" | #ArrayCount(city[])# |
- Sample
<webSet #x[]# = ",0,1,2,3,,5,6,,,20,30"> <webFOR #n# = 0 to 10> <webSet #y[n]# = #int (rnd (2) * 100)#> </webFor>
<webFor #n# = 0 to 10> <webPrint>(#x[n]#, #y[n]#);</webPRINT> </webFor> <p> Nbr. of items in array X: <webPrint>#ArrayCount(x[])#</webPrint>
- Result:
- Exercise
-
You can try out some Web+ code of your own here.
|