Author Topic: Checking for a literal space  (Read 2592 times)

mikelimtw

  • Jr. Member
  • **
  • Posts: 51
Checking for a literal space
« on: March 10, 2017, 04:46:09 PM »
Hi guys,

I'm having just a small problem. I'm using a loop to build a string. I use the {CMD}, {TXTSUBSTR} and {TXTCONCAT} tokens to achieve this, and I want the loop to terminate when the character from {CMD} is evaluated to be an empty space. The code follows:

Start Loop While : [{TXTSUBSTR:"{CMD}":TextPos:1}] Does Not Equal '' ''
  Set integer [TextPos] to [TextPos] plus 1
  Set Text [Char] to '{TXTSUBSTR:"{CMD}":TextPos:1}'
  Set Text [Priority1] to '{TXTCONCAT:Priority1:Char}' (save value to profile)
End Loop

The problem I'm having is that the loop doesn't stop when it reaches the literal space character. In the Loop command I am comparing to the TEXT field which I have populated with a ' ' (empty space) character. Please let me know what I'm doing wrong. Thanks!

Also, how do I set a text variable to a NULL string '' instead of NOT SET?
« Last Edit: March 10, 2017, 04:51:05 PM by mikelimtw »

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4778
  • RTFM
Re: Checking for a literal space
« Reply #1 on: March 10, 2017, 06:02:20 PM »
The problem I'm having is that the loop doesn't stop when it reaches the literal space character.
I have the following:
Code: [Select]
Set Text [Priority1] to ''
Set integer [TextPos] value to 0
Start Loop While : [{TXTSUBSTR:"{CMD}":TextPos:1}] Does Not Equal ' '
    Set Text [Char] to '{TXTSUBSTR:"{CMD}":TextPos:1}'
    Set Text [Priority1] to '{TXTCONCAT:Priority1:Char}'
    Set integer [TextPos] to [TextPos] plus 1
End Loop
Write '[Purple] {TXT:Priority1}' to log
Note that I moved the incrementing of "TextPos" to the bottom, so the first character of the command name doesn't get cut off.

The above works fine on my machine. "New Command 1" produces "New", as expected

An alternative method to accomplish the same result:
Code: [Select]
Set Text [Priority1] to ''
Set integer [TextPos] value to 0
Start Loop While : [{TXTSUBSTR:"{CMD}":TextPos:1}] Does Not Equal ' '
    Set integer [TextPos] to [TextPos] plus 1
End Loop
Set Text [Priority1] to '{TXTCONCAT:Priority1:"{TXTSUBSTR:"{CMD}":0:TextPos}"}'
Write '[Purple] {TXT:Priority1}' to log
Note that the "Use Nested Tokens" option need to be enabled for this, otherwise you need to use an intermediate text variable, as you've done with "Char".

If you don't need the contents of "Priority1" conserved, you can do away with the nested tokens and set it directly to "{TXTSUBSTR:"{CMD}":0:TextPos}".

EDIT: As an aside, unless you need a number that doesn't fall between -32,768 and 32,767, you can use the "Small Integer (Condition)" value rather than an "Integer"


how do I set a text variable to a NULL string '' instead of NOT SET?
NULL and an empty string are not the same thing, but if you just need the latter:
Code: [Select]
Set Text [Text] to ''
« Last Edit: March 10, 2017, 08:33:54 PM by Pfeil »

mikelimtw

  • Jr. Member
  • **
  • Posts: 51
Re: Checking for a literal space
« Reply #2 on: March 10, 2017, 09:12:31 PM »
Hi Pfeil,

I figured it out.. I literally only needed to hit OK or type a space in the Text box. :P
Also, the {TXTSUBSTR} token requires an integer variable and does not work with small integer. I tested it and using TextPos as a small integer causes the routine to fail.
« Last Edit: March 10, 2017, 09:46:16 PM by mikelimtw »

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4778
  • RTFM
Re: Checking for a literal space
« Reply #3 on: March 10, 2017, 10:27:19 PM »
I literally only needed to hit OK or type a space in the Text box.
Start Loop While : [{TXTSUBSTR:"{CMD}":TextPos:1}] Does Not Equal '' ''
Heh. I figured those were double quotes, was wondering why you changed those around.

the {TXTSUBSTR} token requires an integer variable and does not work with small integer.
You could use the "{SMALL:}" token, though admittedly the token processing outweighs the memory usage of the integer any day.