Informing the IBM Community

Head outline and magnifying glass

Language Comparison’s

1.7
(3)

As the landscape of programming continues to evolve, many of us IBM i developers find ourselves exploring a diverse array of languages beyond those with which we began our coding journey.

As more and more of us get involved with different languages I thought it would be a good idea to give a guide to other language’s built in functions and operational codes, or at least 3 of them.
My first programming language was MUMPS (Massachusetts General Hospital Utility Multi-Programming System), running on a Digital Equipment Corporation (DEC) PDP-11 in the very early 80’s. Yes showing my age again!

The ever expanding horizons available on the IBM i mean we are not expected to be solely a RPG programmer these days.
I will show the syntax in the following different languages.

  • RPG
  • CL
  • Node.js and JavaScript
  • PHP
  • Python

Substring

I just had to start the list with my bugbear!  I can never remember the syntax of sub-string in any language. So, a great one to start with as far as I’m concerned!

I’m sure we all know that the sub-string function in any programming language will return part of a string for you but let us look at how it differs in different languages.

The syntax for RPG is as follows.

%SUBST(Source string: start :length to extract)

The first parameter is the source string from which we want to extract some part.

The second parameter is the starting position from where we will start the abstraction of the string, starting at one.

The third, and final parameter, is the length to extract.

Each parameter part is delimited with a colon (:)

CL

The syntax for CL programming is as follows.

%SUBSTRING(string starting-position length)

With CL, the %SUBSTRING function, which can be shortened to %SST, uses a character string as the first parameter, with the second parameter being the starting position in the string (starting with position one) and the third parameter being the length to extract.

Each parameter part is delimited with a space character.

JavaScript

If you are using node or JavaScript, the syntax for the substring function is shown below.

string.substring(start, end)

With JavaScript, the first parameter is the start position.  Note that in JavaScript the starting position starts at zero, as do most of the open-source languages.

The second parameter is the end position count.  Additionally, the second parameter is optional, if it is not passed, the rest of the string is returned.

Each parameter part is delimited with a coma.

PHP

If you are using PHP, the syntax for the substring function is shown below.

substr(string, offset, length)

with PHP, the first parameter is the start position.  Note that in PHP the starting position starts at zero.

Again, each parameter part is delimited with a coma.

Python

Python offers many ways to substring a string. This is often called slicing.

Here is the syntax:

string[start:end:step]

The first parameter is start.  This is the starting index of the substring. The character at this index is included in the substring. If start is not included, it is assumed to equal to 0.

The second parameter is the terminating index of the substring. The character at this index is not included in the substring.

If end is not included, or if the specified value exceeds the string length, it is assumed to be equal to the length of the string by default.

And the third parameter is the step parameter.  Every step character after the current character to be included. The default value is 1. If step is not included, it is assumed to be equal to 1.

Trim

Next, onto the trimming of strings.  I will only include the basic TRIM function here; I will leave it up to you to guess the left and right trims!

RPG

The RPG %TRIM built in function removes characters from the edges of a string.

The syntax for RPG trimming is as follows.

%TRIM(string : characters-to-trim)

If %TRIM is past with only one parameter, it will return the string with any leading and trailing blanks removed.

The second parameter is a string to trim.  For example, if we had £123 as the string, we could use the ‘£’ as the second parameter to be stripped.

Each parameter part is delimited with a colon (:)

CL

The syntax for CL programming trimming is as follows.

%TRIM(string  to-trim-char)

The first parameter is the string to be trimmed and the second is the character to be trimmed.

JavaScript

The syntax of the trim function of JavaScript is shown below.

string.trim()

The trim method of string removes whitespace from both ends of this string and returns a new string, without modifying the original string.

PHP

If you are using PHP, the syntax for the trim function is shown below.

trim(string, characters)

with PHP, the first parameter is the string to trim.

The second, optional, parameter can be used to specify string values to be stripped.

Again, each parameter part is delimited with a coma.

Python

The syntax for Python using the trim function is as follows.

string.trim(characters to trim)

the parameter is optional, a set of characters that you want to remove.

If no parameters are provided, whitespaces would be removed.

Scan

Onto the last function of this article, the SCAN function.

RPG

%SCAN returns the first position of the search argument in the source string, or 0 if it was not found.

The syntax for RPG trimming is as follows.

%SCAN(search argument : source string )

The start position and length specify the substring of the source string to be searched.

The start position defaults to 1 and the length defaults to the remainder of the source string.

The result is always the position in the source string even if the starting position is specified.

Each parameter part is delimited with a colon (:)

CL

Very similar to RPG, the scan built-in function (%SCAN) returns the first position of a search argument in the source string, or 0 if it was not found.

The syntax for CL programming scanning is as follows.

%SCAN(search-argument source-string)

The first parameter is the string to be searched for and the second is the string to be searched.

JavaScript

In JavaScript we can use the includes method which returns true or false depending on whether a character is in a string or not.

includes(string, position)

The first parameter is the string to search for.

The second parameter, which is optional, is the position within the string at which to begin searching.  It defaults to 0.

PHP

In the PHP language we use the str_contains function to determine if a string contains a provided substring.

str_contains(string (haystack), string (needle) )

with PHP, the first parameter is the string to search.

The second parameter is the substring to search for in the haystack

Again, each parameter part is delimited with a coma (,)

Python

In python there are many ways to scan a string.  The method we will show is the in method.

character in string

We can use the in keyword for checking if a character is present in the string. If it is present in the string, True is returned, otherwise, False is returned.

Conclusion

Hopefully, this article has given you a quick insight to some of the common functions of different programming languages.

All the examples I have written for this article, and previous ones, can be found on my companies open-source repository on GitHub, which can be found at https://github.com/formaserve/f_Learning

If you have any questions, either on this article, or anything else on the IBM i, please use the comments below, or send me a message on twitter @AndyYouens

Andy Youens is an IBM i consultant/instructor at Milton Keynes, UK-based FormaServe Systems with over 40 years IBM midrange development experience. 

IBM Champion

How useful was this post?

Click on a star to rate it!

Average rating 1.7 / 5. Vote count: 3

No votes so far! Be the first to rate this post.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *