Websites Navigation: Airbit | Shop | m-shell.net
Languages: EN | DE

Basic Arrays

Our SMS service should examine each incoming SMS and check whether it matches a list of keywords we defined. If a match is found, the corresponding reply should be sent back. Let's assume we initially start with the following keywords and replies:

KeywordReply
partyThe party starts at 8pm!
placeI am at home.
moodJust don't ask.

For instance, if someone sends you an SMS with the text "mood", your phone should automatically reply "Just dont ask.".

In m, we could represent this table as two arrays:

keywords=["party", "place", "mood"];
replies=["The party starts at 8pm!",
         "I am at home.",
         "Just don't ask."]

An array is a collection of values (numbers, strings, other arrays...). The above two statements create two arrays and assign them to the variables keywords and replies.

A few observations may help clarifying:

  • A variable is just a name we can assign a value to. In m, names are case sensitive, so keywords and KeyWords are two different names. Blanks or interpunction cannot be used in names, and they must not start with digits.
  • The = operator assigns a value to a variable.
  • Two assignments (and two statements in general) must be separated by a semicolon (;).
  • An array is defined by a comma separated list of values between brackets ([]).
  • A string must be quoted. Both single quotes (') or double quotes (") can be used.
Single elements of each array can be accessed by indexing :

print keywords[0]
→ party
print replies[2]
→ Just don't ask
print replies[3]
→ ExcIndexOutOfRange thrown
print len(replies) // The number of elements in replies
→ 3

And again a few remarks:

  • Indexing happens by appending the element index between brackets after the array variable.
  • The index of the first element is zero[1].
  • Using an index number for which no element exists is an error: it throws ExcIndexOutOfRange. See section * (Reference) for more information about exceptions.
  • The number of elements (length) of an array can be obtained by calling the len function on the array.
  • Functions are called by their name (e.g. len), followed by the arguments between parentheses ().
  • The rest of the line after two slashes (//) is considered a comment and ignored by m.
Now that we have keywords and replies defined, how are we going to use them? Remember we want to find the reply for an incoming message. This means we have to search through all keywords. If we find a match, the corresponding reply can be used. In m, we could write something like this:

msg=...; // the incoming message
i=0; // start at the first element
while i<len(keywords) and keywords[i]#msg do
  i++
end;
if i<len(keywords) then
  reply=replies[i];
  // send the reply
end

The above code fragment introduces two very important m control structures, while and if:

  • i=0 assigns zero to the variable i.
  • The expression between while and do is evaluated. If it is true, the statements between do and end are executed.
  • i<len(keywords) checks whether i has not yet reached the end of the array.
  • keywords[i]#msg checks whether keywords[i] is not equal to msg.
  • If both conditions are true, we move to the next element: i++ simply adds one to i. We could also have written i=i+1 instead.
  • The while loop ends if either the end of the array has been reached, or keywords[i] equals msg.
  • The expression between if and then is evaluated. If it is true, the statments between then and end are executed:
  • If i<len(keywords), keywords[i] must equal msg, and we reply with the corresponding text replies[i].
As an example, consider msg="place". With i=0, the while condition is true, so i++ is executed, setting i=1. Since keywords[i] now equals msg, the while condition is no longer true. And since i<len(keywords), the reply replies[i] will be sent.


© 2004-2011 airbit AG, CH-8008 Zürich
Document AB-M-TUT-887
mShell Home  > Documentation  > Manuals