Post Reply 
Another question from a newbie
10-25-2019, 02:21 AM
Post: #1
Another question from a newbie
Please look at behavior described below, and tell me whether it's a bug, or I am doing something wrong.
The User's Manual defines behavior of RIGHT as follows:
Returns the last n characters of string str. If n <= 0, returns empty string. If n > DIM(str), returns str
Example: RIGHT("MOMOGUMBO",5) returns "GUMBO"

The attached example clearly shows that the character pointed by the argument is included in the resulting string. Thus, if we point to the last character of the string, we should get the last character of the string as a result. (Technically it will be a string consisting of one character, but let's use the name "character" to avoid confusing input and output strings.)
Please run the following program:
Code:
#pragma mode( separator(.,;) integer(h64) )

EXPORT TEST_RIGHT()
BEGIN

  LOCAL sString :="12D";
  
  PRINT();

  // verify indices
  PRINT("[1]=="+CHAR(sString(1)));
  PRINT("[2]=="+CHAR(sString(2)));
  PRINT("[3]=="+CHAR(sString(3)));

  // verifiy length
  PRINT("Length=="+DIM(sString));

  // attempt extracting substrings
  PRINT(RIGHT(sString,3));  // should return "D", but returns "12D
  PRINT(MID(sString,3,1));  // should return "D", and it does

  WAIT(-1);

END;
The result will be:

[1]==1
[2]==2
[3]==D
Length==3
12D
D

(Run on the simulator.)
In other words, if the RIGHT command is used, and the pointer points to the last character in the string, HPP "thinks" that it points beyond the string, and returns entire string rather than one character.

Darius
Find all posts by this user
Quote this message in a reply
10-25-2019, 05:53 AM
Post: #2
RE: Another question from a newbie
There is a misunderstanding here of what the second argument means. It is not a pointer to a position within the string, but a count of how many characters should be returned, with the counting starting from the right end of the input string.

RIGHT("abcdefg",2) returns "fg" because those are the rightmost two character of the input string.

RIGHT("MOMOGUMBO",5) returns "GUMBO" not because 5 points to the 5th character in the input, but because "GUMBO" is the rightmost 5 characters of the input.

And that's why
RIGHT("MOMOGUMBO",1) returns "O", and
RIGHT("MOMOGUMBO",2) returns "BO", and so on.
RIGHT("MOMOGUMBO",9) returns all 9 characters of the input, as it should.

<0|ΙΈ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
10-25-2019, 12:45 PM
Post: #3
RE: Another question from a newbie
to add to what Joe said, it sounds like what you were wanting to do with RIGHT() can be done with MID() with only one argument following the string.

Code:
LEFT("ABCDEFG",3)   --> "ABC"
RIGHT("ABCDEFG",3)  --> "EFG"
MID("ABCDEFG",3,2)  --> "CD"      starting from the 3rd character, return 2 characters
MID("ABCDEFG",3)    --> "CDEFG"   starting from the 3rd character, return the remaining characters.
Find all posts by this user
Quote this message in a reply
10-25-2019, 01:58 PM
Post: #4
RE: Another question from a newbie
(10-25-2019 05:53 AM)Joe Horn Wrote:  There is a misunderstanding here of what the second argument means. It is not a pointer to a position within the string, but a count of how many characters should be returned, with the counting starting from the right end of the input string.
Okay, now I get it.
The MOGUMBO example happened to work for both approaches (index and position), thus my confusion.
Thanks, Joe!

Darius
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)