Lasso Soft Inc. > Home

  • Articles

Using Arrays to Simplify Code

This article presents several techniques which use arrays to help simplify your Lasso code. An array is used to help format several fields from a database. An array is used to help simplify a conditional. And, an array is used to temporarily sort some data for display.

Arrays For Data Display

When we display data from a database we frequently need to adjust the format depending on what fields are empty. For example, we might display the name and address of a contact using the following code.

  [Field('First Name')] [(Field: 'Last Name')]
  <br />[Field('Company')]
  <br />[Field('Address1')]
  <br />[Field('Address2')]
  <br />[Field('City')], [Field('State')] [Field('Zip')]
  <br />[Field('Country')]
  <br />Phone: [Field('Phone_Number')]
  <br />Email: [Field('Email_Address')]


However, this code will include a lot of blank lines if the second address field is empty, the country is empty, the contact doesn't have an email address, etc.

Instead, we can use an array to simplify the code. We create an array that is going to store our contact information. We then insert each value that we want to display into the array. Before we display the array we eliminate all empty lines.

    Var: 'Contact' = Array;
    $Contact->(Insert: Field('First Name') + ' ' + Field('Last Name'));
    $Contact->(Insert: Field('Company'));
    $Contact->(Insert: Field('Address1'));
    $Contact->(Insert: Field('Address2'));
    $Contact->(Insert: Field('City') + ', ' +
        Field('State') + ' ' + Field('Zip'));
    $Contact->(Insert: Field('Country'));
    Field('Phone_Number') != '' ?
        $Contact->(Insert: 'Phone: ' + Field('Phone_Number'));
    Field('Email_Address') != '' ?
        $Contact->(Insert: 'Email: ' + Field('Email_Address'));


Now the display code is simplified and does not contain any blank lines either in the visitor's Web browser or in the HTML code.

  [Iterate($Contact, Var('contact_temp'))]
    [Var('contact_temp')]<br />


Simplifying Conditionals

It is often necessary to check a single variable against several different values in a conditional. For example, we might want to display a message only on weekdays.

  [Var('dayofweek' = Date->DayOfWeek)]
  [If($dayofweek == 'Monday' || $dayofweek == 'Tuesday' ||
      $dayofweek == 'Wednesday' || $dayofweek == 'Thursday' ||
      $dayofweek == 'Friday')]


Instead we can use an array to simplify the conditional. We store the day names we want to compare against in an array and use the contains symbol >> to check whether the $dayofweek is contained in the array.

  [Var('dayofweek' = Date->DayOfWeek)]
  [Var('weekdays' = Array('Monday', 'Tuesday',
      'Wednesday', 'Thursday', 'Friday'))]
  [If($weekdays >> $dayofweek)]


Sorting Data

Usually, we allow our data source to sort records into the order we want to display them, but sometimes this is not possible. We may want to display the same data using several different sorts. Or, we may have acquired our data from a source like an RSS feed or XML file which doesn't provide sorting capability.

When necessary, we can sort data using a temporary array of pairs. The first part of each pair will contain the value we want to sort on and the second part of each pair will contain the data to display. For example, we might want to sort a list of participants based on the number of points they received in a contest.

    Var('sort_array' = Array);
      $sort_array->Insert(Integer(Field('score')) =
          Field('First Name') + ' ' + Field('Second Name'););


The array will contain data like that shown below. Each pair contains a score and the name of the participant. The values are sorted in descending order of the scores.

  Array(100 = 'John Doe', 87 = 'Jane Doe', 45 = 'Joe Surname');


We can output the data using [Iterate] ... [/Iterate] tags. Note that the variable $sort_temp will be set to the complete pair so we need to extract the ->First and ->Second elements. In the code below we set the variables $score and $name to these elements and then display them.

  [Iterate($sort_array, Var('sort_temp'))]
    [Var('score' = $sort_temp->First, 'name' = $sort_temp->Second)]
    [Var('score')]: [Var('name')]<br />


The results are shown below. Each name is shown with its score.

  100: John Doe<br />
  87: Jane Doe<br />
  45: Joe Surname<br />


More Information

More information about all of the tags used in this tip of the week can be found in the Lasso 8.5 Language Guide or in the online Lasso Reference.

Author: Fletcher Sandbeck
Created: 30 Nov 2007
Last Modified: 24 Mar 2011


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