How to check if a variable defined?

Assistance with (ALMOST) anything not directly related to Program O

How to check if a variable defined?

Postby Alekz » January 26th, 2015, 3:28 pm

Is it possible to check if a variable defined (regardless the content, because it varies) or not?
Alekz
Avid Member
 
Posts: 75
Joined: January 25th, 2015, 2:47 pm

Re: How to check if a variable defined?

Postby GeekCaveCreations » January 28th, 2015, 2:46 pm

I'm not 100% certain what you mean, Alekz. If you're referring to a "client predicate", such as what you get when you use the <get> or <set> tag, then sure. It's rather easy to use the <condition> tag to check the value. Program O uses a default value of "undefined" for all client predicates, so unless a given client predicate is intentionally set to that value, the following AIML category can be used to test if a given predicate (name, for example) has been set:

Code: Select all
<category>
  <pattern>CHECKFORNAME</pattern>
  <template>
    <condition name="name">
      <li value="undefined">You haven't told me your name yet.</li>
      <li value="Dave">Hey! I know another guy named Dave!</li>
      <li value="SomethingSilly">Did your parents SERIOUSLY burden you with that name?!?!?!</li>
      <li><get name="name"/> is a nice name.</li>
    </condition>
  </template>
</category>


Now this is just a simple example of how the <condition> tag can be used, but it gives you a fairly good idea of how to use it to check a client predicate for certain values.

I hope this answers your question. If not, then maybe a bit more information would help. ;)
Comforting the disturbed, and disturbing the comfortable
Chat with Morti
User avatar
GeekCaveCreations
Safe, Reliable Insanity, Since 1961
 
Posts: 1115
Joined: April 18th, 2011, 10:52 pm
Location: Nevada, USA

Re: How to check if a variable defined?

Postby Alekz » January 28th, 2015, 8:39 pm

<li value="undefined">You haven't told me your name yet.</li>

Yes, this is what I tried, but it did not work. I think because in this case the variable "name" is defined and its value "undefined". If a variable is not defined, it does not have any value at all.
Alekz
Avid Member
 
Posts: 75
Joined: January 25th, 2015, 2:47 pm

Re: How to check if a variable defined?

Postby Alekz » January 28th, 2015, 8:49 pm

Hm.. Strange, just tried again, now it works... Weird....

Thanks!
Alekz
Avid Member
 
Posts: 75
Joined: January 25th, 2015, 2:47 pm

Re: How to check if a variable defined?

Postby Alekz » January 28th, 2015, 8:58 pm

Ah, actually, I see why I said that it did not work. Basically, this is what I need:
Code: Select all
<category>
<pattern>vartest1</pattern>
 <template>
    <condition name="name1">
       <li value="*"> Name1 is defined as <get name="name1"/></li>
       <li>Name1 is not defined</li>
    </condition>
 </template>
</category>


But, the construction <li value="*"> does not work as a "match all" wildcard.

And this code:

Code: Select all
<category>
<pattern>vartest3</pattern>
 <template>
    <condition name="name3">
       <li value="undefined">Let's define name3 as test3
          <think><set name="name3">test3</set></think></li>
        <li> Name3 is defined as <get name="name3"/></li>
    </condition>
 </template>
</category>


Always produces the following result:

Code: Select all
User: vartest3
Bot: Name3 is defined as undefined
User: vartest3
Bot: Name3 is defined as undefined
User: vartest3
Bot: Name3 is defined as undefined
Alekz
Avid Member
 
Posts: 75
Joined: January 25th, 2015, 2:47 pm

Re: How to check if a variable defined?

Postby Alekz » January 28th, 2015, 9:30 pm

From what I see in this thread: https://www.chatbots.org/ai_zone/viewthread/1858/ wildcards should be allowed in the value field. Possibly, Program O does not support that?
Alekz
Avid Member
 
Posts: 75
Joined: January 25th, 2015, 2:47 pm

Re: How to check if a variable defined?

Postby Alekz » January 28th, 2015, 9:40 pm

Just checked if <if> tag is implemented in Program O (http://www.alicebot.org/documentation/aiml-reference.html#if) . The answer is No ;)

Code: Select all
<category>
<pattern>vartest4</pattern>
 <template>
   <if name="name4" exists="true">
      Name4 is <get name="name4"/>
   <else/>
      Let's define name4 as test4
      <think><set name="name4">test4</set></think>
   </if>
 </template>
</category>



The result is:

Code: Select all
User: vartest4
Bot: Name4 is undefined Let's define name4 as test4
User: vartest4
Bot: Name4 is test4 Let's define name4 as test4
Alekz
Avid Member
 
Posts: 75
Joined: January 25th, 2015, 2:47 pm

Re: How to check if a variable defined?

Postby GeekCaveCreations » February 19th, 2015, 6:30 pm

You're right about no support for the <if> tag. At some point I was under the impression that it was a deprecated tag, like <gossip> is, so it was never added.
Comforting the disturbed, and disturbing the comfortable
Chat with Morti
User avatar
GeekCaveCreations
Safe, Reliable Insanity, Since 1961
 
Posts: 1115
Joined: April 18th, 2011, 10:52 pm
Location: Nevada, USA


Return to AIML/General help

Who is online

Users browsing this forum: No registered users and 2 guests

cron