Lasso Soft Inc. > Home

  • Tutorials

Using Groups of Information: Arrays and Maps

There is still another way to make our code easier to manage, even in such a simple example.

A good example is, it would be easier to group our content in one place, so that later we don't have to dig through code as our code becomes more complex. Our ongoing example;

[if:(date)>'12:00'&&(date)<'00:00']
[Var: 'Salutation' = 'Good Morning']
[else:(date)>'18:00'&&(date)<'00:00']
[Var: 'Salutation' = 'Good Afternoon']
[else]
[Var: 'Salutation' = 'Good Evening']
[/if]
<body>[$Salutation]! I am an HTML document</body>

 

In this instance, we have a similar type of information (a salutation), which we can bundle together. One of the common ways to bundle information together in one easy place is with an ARRAY. An Array is simply a common set of information we want to bundle together for easy access in the future. Where a VARIABLE is a single piece of information, an ARRAY has another dimension to it - it's a grouping of the same type of information all held in the same bucket.

You would set an array like so;

[Variable: 'Salutation_Array' = (Array: 'Good Morning', 'Good Afternoon', 'Good Evening')]

 

We have a new array called "Salutation_Array", and into it we have put all of our different salutations. We can then do many things with this array. For example, we can get a specific piece of information out of the array;

[(Variable:'Salutation_Array')->(Get:'1')]

 

This code is saying - "go to the salutation array and get me the first thing you see". Another easy/lazy way to write this would be like so;

[$Salutation_Array->(Get:'1')]

 

So we can clean up our code some more by doing the following;

[Variable: 'Salutation_Array' = (Array: 'Good Morning', 'Good Afternoon', 'Good Evening')]
[if:(date)>'12:00'&&(date)<'00:00']
[Var: 'Salutation' = ($Salutation_Array->(Get:'1'))]
[else:(date)>'18:00'&&(date)<'00:00']
[Var: 'Salutation' = ($Salutation_Array->(Get:'2'))]
[else]
[Var: 'Salutation' = ($Salutation_Array->(Get:'3'))]
[/if]
<body>[$Salutation]! I am an HTML document</body>

 

What we have done is set different values for potential salutations in one place (in our array called "Salutation_Array"), added some logic to dynamically set a variable piece of information (in our variable called "Salutation") and displayed it for the user to see. We are getting into real programming here!

Here's where is get's fun. Let's say we wanted to change the background color of the page depending on what time it is. We would do this once like so;

<body style="background-color:red;">Hello! I am an HTML document</body>*

 

*This is using a technique of Cascading Style Sheets, one of the standards of the web.

Now of course, we want the page to be a different color depending on the time of day. So let's suggest some colors;

Afternoon -> red
Evening -> blue
Morning -> yellow

Now here is where another fundamental programming idea comes into place: the "name-value pair". Essentially, many things are defined as a pair in web programming: a descriptive name and and an assigned value. Usually the name of the thing stays the same, and the value changes. What we see above are some "pairs" of information: the period and the color. In our array, we can define it differently. Start with the initial array;

[Variable: 'Salutation_Array' = (Array: 'Good Morning', 'Good Afternoon', 'Good Evening')]

 

Let's write it out a little diferently so we can read it easier;

[Variable: 'Salutation_Array' = (Array: 
'Good Morning', 
'Good Afternoon', 
'Good Evening')]

 

Then let's add some values to the names so the array becomes a pair;

[Variable: 'Salutation_Array' = (Array: 
'Good Morning'='yellow', 
'Good Afternoon'='red', 
'Good Evening'='blue')]

 

As a matter of note, there is another type of array which is better designed to handle pairs of data. Arrays are generally used for groups of single data, and name-value pairs in an array are merely kept as "pairs". This other type is called a "Map". A "Map" is designed exclusively for name-value pairs of information. Try this instead;

[Variable: 'Salutation_Array' = (Map: 
'Good Morning'='yellow', 
'Good Afternoon'='red', 
'Good Evening'='blue')]

 

We can go get this data by simply "getting" the data from the Map. So if we do the following;

[Variable: 'Salutation_Array' = (Map: 
'Good Morning'='yellow', 
'Good Afternoon'='red', 
'Good Evening'='blue')]
[$Salutation_Array->(Get:1)]

 

This would spit out: (Pair: (Good Morning), (yellow))

Which is a pair of data, or a small array! So we can request the first thing in the array, from the first thing in the array;

[Variable: 'Salutation_Array' = (Map: 
'Good Morning'='yellow', 
'Good Afternoon'='red', 
'Good Evening'='blue')]
[($Salutation_Array->(Get:1))->(Get:1)]

 

This would spit out: 'Good Morning'

Now, if we take this metaphor back to our example, we can use a combination of maps, conditions and variable data to dynamically change the message and color of our website with one small script;

[Variable: 'Salutation_Array' = (Map: 
'Good Morning'='yellow', 
'Good Afternoon'='red', 
'Good Evening'='blue')]
[if:(date)>'12:00'&&(date)<'00:00']
[Var: 'Salutation' = (($Salutation_Array->(Get:'1'))->(Get:1))]
[Var: 'Color' = (($Salutation_Array->(Get:'1'))->(Get:2))]
[else:(date)>'18:00'&&(date)<'00:00']
[Var: 'Salutation' = (($Salutation_Array->(Get:'2'))->(Get:1))]
[Var: 'Color' = (($Salutation_Array->(Get:'2'))->(Get:2))]
[else]
[Var: 'Salutation' = (($Salutation_Array->(Get:'3'))->(Get:1))]
[Var: 'Color' = (($Salutation_Array->(Get:'3'))->(Get:2))]
[/if]

<body style="background-color:[$Color];">[$Salutation]! I am an HTML document</body>

 

Of course, we have now made a document which dynamically changes colors and messaging depending on the time of day. We have a dynamic website. We should really look at who is coming to the website and make sure we are changing the site to meet their needs.

Next Tutorial: Interacting with a Browser

Comments

No comments found
You must be logged in to comment.

Please note that periodically LassoSoft will go through the notes and may incorporate information from them into the documentation. Any submission here gives LassoSoft a non-exclusive license and will be made available in various formats to the Lasso community.

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft