Author Topic: Delete a map key once set?  (Read 6543 times)

0 Members and 1 Guest are viewing this topic.

Methos

  • Jr. Member
  • **
  • Posts: 14
  • Karma: 0
    • View Profile
Delete a map key once set?
« on: July 04, 2014, 07:50:57 PM »
Is it possible to delete a map key,value pair once it is set? If not, can you empty it?

Also, it seems that variables are global, as in set var.a in one command, ref it in another and it has the same value. Is there a way to make it local or confined to the command or command group? I know I could name each variable different, this is really just me wondering.

Thanks.

Methos

  • Jr. Member
  • **
  • Posts: 14
  • Karma: 0
    • View Profile
Re: Delete a map key once set?
« Reply #1 on: July 04, 2014, 08:05:50 PM »
So I tried something...

I know if you set a variable in a command and want to reuse the same variable but have it blank every time the command is executed you simply set the var to nothing on the first line of the command. Like so:

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.0.0.0-->
<command id="172" name="variable test" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>Results.SetVar</cmdType>
    <params>
      <param>a1</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.ShowText</cmdType>
    <params>
      <param>Before: {var.a1}.</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Results.SetVar</cmdType>
    <params>
      <param>a1</param>
      <param>{1}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.AddText</cmdType>
    <params>
      <param>After: {var.a1}.</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>variable test</phrase>
  <payloadDictation>payloadDictation: Regular</payloadDictation>
</command>

I tried to do the same thing with Map.Set, but it did not work and gave me an error in the history saying it expected a 3rd parameter. So I thought, maybe if I combine the variable trick it would work, so I tried the following:

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.0.0.0-->
<command id="184" name="map key test" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>Results.SetVar</cmdType>
    <params>
      <param>a1</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Map.Set</cmdType>
    <params>
      <param>Test</param>
      <param>a1</param>
      <param> {var.a}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.ShowText</cmdType>
    <params>
      <param>Before: {M:Test..a1}.</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Map.Set</cmdType>
    <params>
      <param>Test</param>
      <param>a1</param>
      <param>{1}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.AddText</cmdType>
    <params>
      <param>After: {M:Test.a1}.</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>map key test</phrase>
  <payloadDictation>payloadDictation: Regular</payloadDictation>
</command>

Now it does not give me an error in the history window but instead of a blank I get "map.error". Any ideas?
« Last Edit: July 04, 2014, 08:08:10 PM by Methos »

nime5ter

  • Administrator
  • Hero Member
  • *****
  • Posts: 2009
  • Karma: 61
    • View Profile
    • Getting Started with VoxCommando
Re: Delete a map key once set?
« Reply #2 on: July 04, 2014, 08:42:21 PM »
You need to create the map table first with the Map.CreateTable action (unless you've already created a table by that name).

I had no problems with your command once I added that line.

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.0.0.0-->
<command id="184" name="map key test" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>Map.CreateTable</cmdType>
    <params>
      <param>Test</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Map.Set</cmdType>
    <params>
      <param>Test</param>
      <param>a1</param>
      <param />
      <param>True</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.ShowText</cmdType>
    <params>
      <param>Before: {M:Test.a1}.</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Map.Set</cmdType>
    <params>
      <param>Test</param>
      <param>a1</param>
      <param>{1}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.AddText</cmdType>
    <params>
      <param>After: {M:Test.a1}.</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>map key test</phrase>
  <payloadDictation>payloadDictation: Regular</payloadDictation>
</command>

In the history window, whether or not you get an error message, you can usually obtain more information about each log entry by rolling over it. Run your command again as you posted it above, and take a look. You should see a message along the lines of "No such table Test". (Unless you've since created that table.)

wrt your earlier question -- yes, you're right about variable names. They're global, but volatile. Meaning that they won't be stored if you exit and re-open VC. Whereas map values are stored until you delete or overwrite them.

You can delete map key-value pairs directly in the map editor if you want to get rid of both. Or you can overwrite an individual key, or you can probably use Map.Query as well if you're comfortable with sql queries.
TIPS: POST VC VERSION #. Explain what you want VC to do. Say what you've tried & what happened, or post a video demo. Attach VC log. Link to instructions followed.  Post your command (xml)

nime5ter

  • Administrator
  • Hero Member
  • *****
  • Posts: 2009
  • Karma: 61
    • View Profile
    • Getting Started with VoxCommando
Re: Delete a map key once set?
« Reply #3 on: July 04, 2014, 08:45:21 PM »
by the way, you had some typos in your second command above, which probably contributed to some of the problems you've been having with your testing.
TIPS: POST VC VERSION #. Explain what you want VC to do. Say what you've tried & what happened, or post a video demo. Attach VC log. Link to instructions followed.  Post your command (xml)

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Delete a map key once set?
« Reply #4 on: July 04, 2014, 08:55:37 PM »
Yes you can delete map entries using the Map.Query action

Here is some example code.
Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.0.0.0-->
<commandGroup open="True" name="map delete example" enabled="True" prefix="" priority="0" requiredProcess="" description="">
  <command id="959" name="map create test data" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>Map.CreateTable</cmdType>
      <params>
        <param>test</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>Map.Set</cmdType>
      <params>
        <param>test</param>
        <param>key{i}</param>
        <param>valuu{i}</param>
      </params>
      <cmdRepeat>10</cmdRepeat>
    </action>
  </command>
  <command id="958" name="map delete key 5" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>Map.Query</cmdType>
      <params>
        <param>delete from test where fromKey="key5"</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
  </command>
</commandGroup>

One command that creates ten dummy values and one command that deletes the 5th one.  In this case the Map.Query action will return an error but it actually works as intended.  The error is because Map.Query assumes you are trying to get a value back and nothing is returned after you delete an item.

Just to confirm what N. said:  All VC variables created using Results.SetVar are global (actually stored in a dictionary with the variable name as the key) and there is no way to make them local.  If you really need a local variable then you can set them to blank values, or at some point you may just want to use Python in which case you have the choice or using local or global variables as you prefer.

Methos

  • Jr. Member
  • **
  • Posts: 14
  • Karma: 0
    • View Profile
Re: Delete a map key once set?
« Reply #5 on: July 04, 2014, 09:27:37 PM »
Oh yes, I didn't realize I had that second (.), thanks!!  :bonk

And thank you James for your example, this is an awesome program.

Quick question, I know its $30 for ver 1 and $40 for ver 1 & 2, is there a ver 2 only option, or not till the official ver 2 fully comes out?

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Delete a map key once set?
« Reply #6 on: July 04, 2014, 11:03:46 PM »
Version 1 is a subset of version 2, so no, there is no option for "version 2 only".  Pretty much anyone who buys version 2 will be using version 2 only I should think.  $40 is just the cost for the program.  The only thing that will happen when I officially release version 2 is that I will stop offering version 1 as an option.

Methos

  • Jr. Member
  • **
  • Posts: 14
  • Karma: 0
    • View Profile
Re: Delete a map key once set?
« Reply #7 on: July 05, 2014, 12:58:58 AM »
I still couldn't get my code to work (after taking care of the typos) so I compared mine to the code you gave, Nime5ter, and I found something out.

On the line where we are blanking the key, I had left out the "True" on the 4th parameter because when I read the wiki it said the default for the overwrite field was true, so I thought I could leave it blank and make it use the default.
This caused the "Error: expected 3 parameters" to show up, and the value would not clear. When I typed "true" into the box, it worked.

So then I thought of adding another parameter after the Overwrite field and typed random text into it and deleted the "true" from the overwrite field, and it still worked.

I guess Vox was just looking for some kind of 3rd parameter and it didn't matter where it came from. Just wanted to give a heads up, in case this might work in a similar situation.

Here is the code demonstrating what I mean:

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.0.0.0-->
<command id="185" name="map key test" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
  <action>
    <cmdType>Map.CreateTable</cmdType>
    <params>
      <param>Test</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Map.Set</cmdType>
    <params>
      <param>Test</param>
      <param>a1</param>
      <param />
      <param />
      <param>dsfds</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.ShowText</cmdType>
    <params>
      <param>Before: {M:Test.a1}.</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>Map.Set</cmdType>
    <params>
      <param>Test</param>
      <param>a1</param>
      <param>{1}</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <action>
    <cmdType>OSD.AddText</cmdType>
    <params>
      <param>After: {M:Test.a1}.</param>
    </params>
    <cmdRepeat>1</cmdRepeat>
  </action>
  <phrase>map key test</phrase>
  <payloadDictation>payloadDictation: Regular</payloadDictation>
</command>

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Delete a map key once set?
« Reply #8 on: July 05, 2014, 09:03:02 AM »
The command builder will "clean up" (discard) completely empty parameter fields at the end of an action, so if you want to force a parameter to be an empty string you need to have another non-blank parameter field to the right of it.

there are 4 different states of being that one could consider "blank" or "empty" and they are all different and need to be handled differently.

1 - non existant - undeclared
2 - declared as existing but not having a value  - i.e. null
3 - an empty string
4 - a string with a blank (space) in it - (or you could use some other string to indicate blankness if you handle it correctly)

For example in the case of a map, if you set a map value to be an empty string, this will return an empty string when you try to access it, but if the map value was never declared, or if you deleted the value using the query I posted earlier, then trying to access the map value will throw an error.

Edit note: Case #2 doesn't really come up when using VC.  It is more of a behind the scenes state but you will encounter it if using Python.  Python uses the keyword none instead of null

Methos

  • Jr. Member
  • **
  • Posts: 14
  • Karma: 0
    • View Profile
Re: Delete a map key once set?
« Reply #9 on: July 05, 2014, 10:46:15 AM »
Thanks for the explanation James. I having another issue and figured it was related to what we are talking about so I included it here.

So earlier in this thread I posted code to blank a variable, show that it blanked the value, then assign a value and show the new value. Each time the command is executed it blanks it, and it works with no problem. Then later on in the night I found code to ping a ip and TTS speak if its working or not, and that works fine with no problem. The issue came up when I went back to run the variable code again. It would not blank. Below is the code, let me know if there is something I am doing wrong.

In order to replicate, launch the "variable test" command first, it will work. Then run the "Ping" command, that will work, then re-run the "variable test" command and the variable will now be set to the ping results and will ignore the blanking.

A note neither command references the same variables nor are they in the same command group (I combined them here just to paste easier).

Code: [Select]
<?xml version="1.0" encoding="utf-16"?>
<!--VoxCommando 2.0.0.0-->
<commandGroup open="True" name="Issue" enabled="True" prefix="" priority="0" requiredProcess="" description="">
  <command id="215" name="variable test" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>Results.SetVar</cmdType>
      <params>
        <param>a1</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>OSD.ShowText</cmdType>
      <params>
        <param>Before: {var.a1}.</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>Results.SetVar</cmdType>
      <params>
        <param>a1</param>
        <param>{1}</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>OSD.AddText</cmdType>
      <params>
        <param>After: {var.a1}.</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <phrase>variable test</phrase>
    <payloadDictation>payloadDictation: Regular</payloadDictation>
  </command>
  <command id="420" name="Ping" enabled="true" alwaysOn="False" confirm="False" requiredConfidence="0" loop="False" loopDelay="0" loopMax="0" description="">
    <action>
      <cmdType>Launch.Capture</cmdType>
      <params>
        <param>C:\Windows\System32\ping.exe</param>
        <param>8.8.8.8</param>
        <param>True</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>Results.RegEx</cmdType>
      <params>
        <param>bytes=(.)* time(.)*ms </param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <action>
      <cmdType>Results.SetVar</cmdType>
      <params>
        <param>laptopOn</param>
        <param>{#M}</param>
      </params>
      <cmdRepeat>1</cmdRepeat>
    </action>
    <if ifBlockDisabled="False" ifNot="False">
      <ifType>(A)==(B)</ifType>
      <ifParams>{var.laptopOn}&amp;&amp;4</ifParams>
      <then>
        <action>
          <cmdType>TTS.SpeakSync</cmdType>
          <params>
            <param>It's working.</param>
          </params>
          <cmdRepeat>1</cmdRepeat>
        </action>
      </then>
      <else>
        <action>
          <cmdType>TTS.SpeakSync</cmdType>
          <params>
            <param>There is a problem!</param>
          </params>
          <cmdRepeat>1</cmdRepeat>
        </action>
      </else>
    </if>
    <phrase>run ping test</phrase>
  </command>
</commandGroup>

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Delete a map key once set?
« Reply #10 on: July 05, 2014, 11:59:02 AM »
Your action to set the variable is not "blanking" it, it is performing the action as per the documentation.  The variable is being set to LastResult as it should.  LastResult is also global.

http://voxcommando.com/mediawiki/index.php?title=Actions#SetVar

Quote
* If <Value> is omitted then the value of {LastResult} will be used as the <Value>

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Delete a map key once set?
« Reply #11 on: July 05, 2014, 12:00:53 PM »
I recommend you give up on trying to use these so called empty or blank variables.  What is the purpose really?

I would just use a single space instead, or something like "--" or "nil" etc.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Delete a map key once set?
« Reply #12 on: July 05, 2014, 12:06:00 PM »
If it is really necessary to use blank variables, then maybe it would help if I created a new variable:

{Emtpy} which is equal to an empty string "".

Methos

  • Jr. Member
  • **
  • Posts: 14
  • Karma: 0
    • View Profile
Re: Delete a map key once set?
« Reply #13 on: July 05, 2014, 02:53:08 PM »
Ok, sorry if I was being annoying about this. I just like to see what I can and cannot do then come up with ideas based on what I've learned.

Thanks again for all your help.

jitterjames

  • Administrator
  • Hero Member
  • *****
  • Posts: 7713
  • Karma: 116
    • View Profile
    • VoxCommando
Re: Delete a map key once set?
« Reply #14 on: July 05, 2014, 03:37:23 PM »
 No worries. You aren't annoying me at all.  ;D