Author Topic: Reading parts of strings  (Read 1992 times)

0 Members and 2 Guests are viewing this topic.

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Reading parts of strings
« on: April 08, 2014, 05:39:05 AM »
hi
I downloaded VC few days go ... since then I can't go to sleep  ;D ... I build in 3 days what i was planing to build in 3 years  ::banana
the software is just magic ... using it with kinect and the response pretty good till now ...

I am wondering how can I read parts of strings ... lets say I Have "A01BF"

I need to read the first character ... then the 4th and 5th ...

thanks
When Voice command gets tough, use hand gestures

Kalle

  • $upporter
  • Hero Member
  • *****
  • Posts: 2320
  • Karma: 47
    • View Profile
Re: Reading parts of strings
« Reply #1 on: April 08, 2014, 06:01:45 AM »
Hi Haddood and welcome to VC-Forum. Nice to hear that you have fun with VoxCommando  :D

Please give us more details in what you plan to do when you say "... how can I read parts of strings ..." - which strings do you mean? Is the string a result from a command?

Anyway, there are many possibilities.
***********  get excited and make things  **********

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Re: Reading parts of strings
« Reply #2 on: April 08, 2014, 06:43:21 AM »
Hi kalle,
yes the string is a result from a command ...  in fact it is the status feedback of insteon devices ... I managed to cut it down to 5 characters only using result.replace. That was kind of easy as there is clear patterns ... now I need to read the last 2 characters that will give if the device is on, off or dim level ...

ex "B10FF" is fully on as FF is the level (Hex)
When Voice command gets tough, use hand gestures

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Reading parts of strings
« Reply #3 on: April 08, 2014, 08:35:30 AM »
Ahhh the old RegEx game.  Who wants to play?  :biglaugh

There are 2 (probably more) ways to handle the goal of capturing the last 2 characters of a string.  You can either use Results.RegEx (preferred) or Results.RegExReplace.

(edited this, got it backwards the first time!)
With Results.RegEx you end up with your 2 chars in {Match.1}
With Results.RegExReplace you end up with your 2 chars in {LastResult}

Here is the xml showing both ways.

Some additional notes:
1 - I am using Results.SetLastResult at the beginning to simulate whatever you would be getting back form your device
2 - While I am at it, this seems like a good way to show how we can use a simple Python action to convert Hex values (from 00 to FF) into a percentage.  If you want to do this you will need to first make sure that the Python plugin is enabled.

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 1.9.1.8-->
<commandGroup open="True" name="last 2 chars" enabled="True" prefix="" priority="0" requiredProcess="" description="">
  <command id="277" name="last2chars using RegExReplace" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>Results.SetLastResult</cmdType>
      <params>
        <param>thang thank doodad stuff B109F</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>Results.RegEx</cmdType>
      <params>
        <param>(..)$</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>result = round(0x{Match.1}/2.55,0)</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>OSD.ShowText</cmdType>
      <params>
        <param>Level is: {LastResult} percent.</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
  </command>
  <command id="290" name="last2chars using RegExReplace" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>Results.SetLastResult</cmdType>
      <params>
        <param>thang thank doodad stuff B109F</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>Results.RegExReplace</cmdType>
      <params>
        <param>.*(..)$</param>
        <param>$1</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>PY.ExecString</cmdType>
      <params>
        <param>result = round(0x{LastResult}/2.55,0)</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>OSD.ShowText</cmdType>
      <params>
        <param>Level is: {LastResult} percent.</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
  </command>
</commandGroup>

http://voxcommando.com/mediawiki/index.php?title=XML_on_the_forum

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Re: Reading parts of strings
« Reply #4 on: April 08, 2014, 04:42:47 PM »
Sweet ....
Another couple of questions:
1. where I can learn about those patern , what if I wanted to extract the 7th character out of 20 characters long string?
2. I tried to convert to hex from decimal using python int( decimal var, 16). And it did not work

Just a note abt my programming experience... I used vb, autolisp, lua scripting ... So python is a bit strange for me
When Voice command gets tough, use hand gestures

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7714
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Reading parts of strings
« Reply #5 on: April 08, 2014, 05:02:56 PM »
Sweet ....
Another couple of questions:
1. where I can learn about those patern , what if I wanted to extract the 7th character out of 20 characters long string?
2. I tried to convert to hex from decimal using python int( decimal var, 16). And it did not work

Just a note abt my programming experience... I used vb, autolisp, lua scripting ... So python is a bit strange for me

1 - http://voxcommando.com/mediawiki/index.php?title=Actions#RegEx
2 - I would need to know in more detail what you actually did in order to assist.  The example I posted successfully converts a hex number to decimal.  As far as different programming languages go, to me they are all about the same.  I can't remember any of them properly and generally have to rely on web searches for most things to refresh my memory.
« Last Edit: April 08, 2014, 06:06:14 PM by jitterjames »

Kalle

  • $upporter
  • Hero Member
  • *****
  • Posts: 2320
  • Karma: 47
    • View Profile
Re: Reading parts of strings
« Reply #6 on: April 08, 2014, 06:27:27 PM »
Quote
2. I tried to convert to hex from decimal using python int( decimal var, 16). And it did not work

https://www.google.de/search?q=python+convert+decimal+to+hex+string

 ::)
« Last Edit: April 08, 2014, 06:29:29 PM by Kalle »
***********  get excited and make things  **********

Haddood

  • $upporter
  • Hero Member
  • *****
  • Posts: 688
  • Karma: 22
    • View Profile
Re: Reading parts of strings
« Reply #7 on: April 09, 2014, 10:48:24 PM »
Thank you guys ... it is done  ::banana
When Voice command gets tough, use hand gestures