Author Topic: Possible bug with EXP tokens  (Read 6392 times)

JGFarris

  • Newbie
  • *
  • Posts: 49
    • My Facebook Page
Possible bug with EXP tokens
« on: October 22, 2016, 07:33:05 PM »
Okay, I think I found a strange little bug. I'm doing a little text manipulation trying to decipher the hour and minute of a string such as "2:04". Take a look at the following script:

Set Text [at_word] to '2:04'
Set Text [at_look for] to ':'
Set integer [at_txtPos] value to 0
Set Text [at_position] to '{TXTPOS:at_look for:at_word:at_txtPos}'
Write 'at_position = "{TXT:at_position}"' to log
Set Text [at_hour] to '{EXP:SUBSTRING('{TXT:at_word}', 1, {TXT:at_position})}'
Set Text [at_minute] to '{EXP:SUBSTRING('{TXT:at_word}', {TXT:at_position} + 2, 2)}'
Write 'Hour is "{TXT:at_hour}". Minute is "{TXT:at_minute}".' to log

When you run this script, it outputs the hour and minute to the event log. However, it evaluate the minute as "4" instead of "04". This is not a mistake in the text manipulating itself. For example if you use a time such as "2:34", then the minute will evaluate correctly as "34". It APPEARS that when the text variable is assigned by the expression, the expression is seeing it as a number, and dropping the 0. Try it yourself, and see what you think.
Jerry Farris, Jr.
Entrepreneur, Programmer and Disability Advocate
jerry@jerryfarris.com

JGFarris

  • Newbie
  • *
  • Posts: 49
    • My Facebook Page
Re: Possible bug with EXP tokens
« Reply #1 on: October 22, 2016, 08:21:13 PM »
I should have better illustrated my theory. As a programmer, I know better. My apologies. Take a look at this.

Set Text [myResult] to '{EXP:TRIM(' 04 ')}'
Write '{TXT:myResult}' to log

The result should be "04", right? It's "4". Is this a bug, or should I be using another method to ensure that it stays text?
Jerry Farris, Jr.
Entrepreneur, Programmer and Disability Advocate
jerry@jerryfarris.com

Gary

  • Administrator
  • Hero Member
  • *****
  • Posts: 2826
Re: Possible bug with EXP tokens
« Reply #2 on: October 22, 2016, 10:43:04 PM »
Hi, Jerry.

Yeah... it looks like EXP is treating that as number.   I'd have to do a code dive on that as well as figure out exactly all that thing does again  lol.

 You might want to try using the token trim feature.  In the  more current betas, you can use a variable, token (between double quotes) or a literal (between double quotes) :  {TXTTRIM:"  04  "} or {TXTTRIM:myTextVariable} will yield what you are expecting without doing more evaluating like EXP does.

Gary

  • Administrator
  • Hero Member
  • *****
  • Posts: 2826
Re: Possible bug with EXP tokens
« Reply #3 on: October 22, 2016, 10:56:22 PM »
No... I'm going to say that EXP is working as expected.  It's an expression evaluator, but there is no way to indicate literal text in the expression.  It then in turn sees, '04' as a number and evaluates it as 04.  It's kind of a tricky bit.  The token method I mentioned previously should do what you are wanting.

JGFarris

  • Newbie
  • *
  • Posts: 49
    • My Facebook Page
Re: Possible bug with EXP tokens
« Reply #4 on: October 23, 2016, 03:46:57 PM »
Well, using the TXTTRIM token would be great if trimming something is what I actually need to do. In my example given, I'm using the substring part of the exp. So, how would I do the following?

Set Text [at_minute] to '{EXP:SUBSTRING('{TXT:at_word}', {TXT:at_position} + 2, 2)}'

This should return the minute portion of a string, "2:04". (See the complete code example in the beginning of this thread). It's simply returning 4. This obviously throws off programming later. You wouldn't believe how long it took me to track this down. LOL. I just knew I had something messed up in the logic of the text manipulation code. :-)

So, is there a simple workaround?
Jerry Farris, Jr.
Entrepreneur, Programmer and Disability Advocate
jerry@jerryfarris.com

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4747
  • RTFM
Re: Possible bug with EXP tokens
« Reply #5 on: October 23, 2016, 04:22:05 PM »
is there a simple workaround?
I don't know about simple, but you could use "LEN" to check the amount of digits and prefix a "0" if there's only one.

JGFarris

  • Newbie
  • *
  • Posts: 49
    • My Facebook Page
Re: Possible bug with EXP tokens
« Reply #6 on: October 23, 2016, 07:36:49 PM »
is there a simple workaround?
I don't know about simple, but you could use "LEN" to check the amount of digits and prefix a "0" if there's only one.

Yes, I guess that's one way to do it.
Jerry Farris, Jr.
Entrepreneur, Programmer and Disability Advocate
jerry@jerryfarris.com

Gary

  • Administrator
  • Hero Member
  • *****
  • Posts: 2826
Re: Possible bug with EXP tokens
« Reply #7 on: October 23, 2016, 07:59:37 PM »
If you hold tight for another 20 mins, I'll have a new {TXTSUBSTR:text:start:length} token for use, outside of EXP

Gary

  • Administrator
  • Hero Member
  • *****
  • Posts: 2826
Re: Possible bug with EXP tokens
« Reply #8 on: October 23, 2016, 08:49:56 PM »
New beta is out there with {TXTSUBSTR:text:start:length} token.  Each parameter can be either a variable (the first param would be a text variable, the other two integer variables (not small integer)), a literal (the text parameter must be between double quotes) or a token (again, the text one must be between double quotes).

So, for your example either of these should work:

{TXTSUBSTR:at_word:{EXP:{TXT:at_position} + 2}:2} 

or

Set integer [myIntStart] value to the converted value of {TXT:at_position}
Set integer [myIntStart] to [myIntStart] plus 2
Write ' {TXTSUBSTR:at_word:myIntStart:2}' to log

Beta can be found here:  http://www.voiceattack.com/beta

JGFarris

  • Newbie
  • *
  • Posts: 49
    • My Facebook Page
Re: Possible bug with EXP tokens
« Reply #9 on: October 24, 2016, 10:39:44 AM »
Fantastic.  I was actually on my way here to the forum to suggest just that. LOL. Thanks Gary. You da Man! :-)
Jerry Farris, Jr.
Entrepreneur, Programmer and Disability Advocate
jerry@jerryfarris.com

JGFarris

  • Newbie
  • *
  • Posts: 49
    • My Facebook Page
Re: Possible bug with EXP tokens
« Reply #10 on: October 24, 2016, 04:26:03 PM »
hey, just real quick wanted to point out a typo in the documentation on the TXTSUBSTR token.

{TXTSUBSTR:”We are the Champions”:11, 9} will render as, ‘Champions’ (note that, ‘Champions’ starts at position 12, but since the beginning position is zero-based, the value is 11).

There is a comma between 11 and 9. Should be a colon.
Jerry Farris, Jr.
Entrepreneur, Programmer and Disability Advocate
jerry@jerryfarris.com

Gary

  • Administrator
  • Hero Member
  • *****
  • Posts: 2826
Re: Possible bug with EXP tokens
« Reply #11 on: October 24, 2016, 04:57:10 PM »
Lol! Force of habit, I suppose :)

Thanks... all fixed up!