Author Topic: Splitting txt variable  (Read 810 times)

Dumbo

  • Newbie
  • *
  • Posts: 2
Splitting txt variable
« on: September 04, 2020, 08:13:19 AM »
I had a quick browse around to find an already answered solution to my problem and found this reply

This can be done using "{TXTSUBSTR:}" and "{TXTPOS:}" tokens:
Code: [Select]
Set Text [grid] to 'B 13'
Set Text [row] to '{TXTSUBSTR:grid:0:{TXTPOS:" ":grid:0}}'
Set Text [column] to '{TXTSUBSTR:grid:{EXP:{TXTPOS:" ":grid:0}+1}:}'
Write [Blue] 'Row = '{TXT:row}', Column = '{TXT:column}'' to log

Which works perfectly but I'm not sure how to scale it to 4? (E.g. grid = "b 13 lunar bingo")

Many thanks for any insight!

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Splitting txt variable
« Reply #1 on: September 04, 2020, 08:22:46 AM »
While that example partially demonstrates the techniques you'll need, it's not intended to be scalable.

If you want to split a string manually like this, you need to look for the next occurrence of the delimiter you're looking to split on, store the text before that location in a variable, store the position of the found delimiter, look for the next delimiter position, store the text between those delimiters to a variable, store the position of the found delimiter, and so on.


In what context are you looking to split this text? If it's for a command phrase you're speaking, you could use the "{CMDSEGMENT:}" token instead.

Dumbo

  • Newbie
  • *
  • Posts: 2
Re: Splitting txt variable
« Reply #2 on: September 04, 2020, 09:42:01 AM »
In what context are you looking to split this text? If it's for a command phrase you're speaking, you could use the "{CMDSEGMENT:}" token instead.

Yes it's basically to split a spoken command but due to the number of dynamic commands it would generate  (26^4 ... lol) I've been looking at other ways to accomplish it. Basically when a command "alpha charlie november mike" is spoken, or any variation of the phoentics  x 4 is said, I have nothing to catch it with so when that happens I use a catch all to then try and disect it and move forward with the information I need.

Hope that makes sense and thank you for replying :)

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Splitting txt variable
« Reply #3 on: September 04, 2020, 10:15:58 AM »
This is one way to do it:
Code: [Select]
Set text [~input] to 'alpha charlie november mike'
Set text [~separator] to ' '

Set integer [~sectionStart] value to 0
Set integer [~separatorCount] value to the converted value of {TXTOCCURRENCES:~separator:~input}
Start Loop : Repeat From 0 to [~separatorCount]
    Set integer [~sectionEnd] value to the converted value of {TXTPOS:~separator:~input:~sectionStart}
    Begin Integer Compare : [~sectionEnd] Equals -1
        Set integer [~sectionEnd] value to the converted value of {TXTLEN:~input}
    End Condition
    Set text [~section{INT:~i}] to '{TXTSUBSTR:~input:~sectionStart:{EXP: {INT:~sectionEnd} - {INT:~sectionStart}}}'
    Set integer [~sectionStart] value to the value of [~sectionEnd]
    Set integer [~sectionStart] to [~sectionStart] plus 1
    Write [Blue] '{TXT:~section{INT:~i}}' to log
End Loop

It'll automatically determine the amount of sections by counting the separators; If that's not a desirable feature, change the "To" value of the "Loop Start - Repeat a Certain Number of Times" action to a literal number instead.

Note that you need to have that action set "~i" as well.