Does a Piece of Text Contain a Phrase?
In this first simple example we look at how to check if a phrase is found within a piece of text that has been stored in a variable.
put "Hello" is in theText
The put command in LiveCode is used to place something into a container such as a variable, file or web page. Note that you don’t need to declare theText variable or specify it’s type. We can choose a destination by adding “into”, e.g. put x into y. We could compare this command in a number of contexts but in this case we will compare the results from using this command in LiveCode Server, where the output will be output directly to the webpage being loaded.
Compared with JavaScript:
document.write(theText.match("World")!=null);
If you’re experienced with JavaScript then the second example may look more familiar to you. Any talented programmer can construct JavaScript like this with ease. But familiarity with something doesn’t always make it the best way to do it. Consider just how much more complex this line of JavaScript is to construct. And how much more prone it is to error: if you leave out a symbol or the semicolon at the end you’ll have to go back and correct it. It also requires considerably more effort to read and maintain later. And this is a very simple example.
Retrieve Words From a Paragraph
In this example we retrieve words 3 to 6 from a paragraph. This problem comes up in applications that pull data together and format from difference sources. For example, you might use code like this to write an email merge application, a log processing utility or a data analysis tool.
In LiveCode:
put word 3 to 6 of “The quick brown fox jumped over the lazy dog.” into theVariable
Compared with Java:
String [] theWords = "The quick brown fox jumped over the lazy dog".split(" "); String theVariable = ""; for(int i = 2; i <= 5; i++) { theVariable += theWords[i] + " "; }
As well as being longer and more complex, the Java example contains many more symbols. Generally speaking symbols detract from the readability of code, and there is a direct correlation between the number of symbols and the number of mistakes it’s possible to make before even running it.
See if Text Begins with a Phrase
In this example we are checking to see if some text begins with a phrase. This problem comes up when checking if the format of unstructured data matches what you are expecting.
In LiveCode:
put theText begins with “Hello”
In JavaScript:
theText.substring(0, "Hello".length).match("Hello") != null;
Even an experienced programmer will need to spend a moment constructing that line in JavaScript. It’s not readable and thus if you plan to maintain your code or hand it to another developer you will need to add a comment to the JavaScript example. Remember, most developers spend more time reading code than they do writing it.
Now look at what happens when we add a little more detail. The specification changes and you realize you want to check if line 3 of the text begins with “Hello”, rather than the text as a whole. In LiveCode this comes out exactly as you would expect:
put line 3 of theText begins with “Hello”
Now look at what happens to the JavaScript:
var theLines = theText.split("n"); theLines[2].substring(0, "Hello".length).match("Hello") != null;
Even an experienced programmer may not always write that correctly first time. And if you need to change that code in the future you will spend at least a moment studying it to understand what each of those function calls is doing before you can change it.
Those of you that are experienced with JavaScript may spot an additional problem with the JavaScript example: it will return an error if there are less than 3 lines. The LiveCode example won’t, it will simply return “false” as you would expect. So you should really add a check to see that you have 3 lines to the JavaScript version, at which point it makes more sense to write this as a JavaScript function:
function beginsWith(theText, theSearch, theLine) { var theLines = theText.split("n"); return (theLines.length >= theLine && theLines[theLine - 1].substring(0, theSearch.length).match(theSearch) != null); }
Whether you need to produce a quick utility that parses data in a few text files, or a complex scalable enterprise IT solution, it could be much faster, easier (even more fun!) to write it in LiveCode.
For those of you that are familiar with PHP, here is the equivalent in that language:
function beginsWith($theText, $theSearch, $theLine) { $theLines = explode("n", $theText); return (substr($theLines[$theLine - 1], 0, strlen($theSearch)) == $theSearch); }