AI Zone Admin Forum Add your forum

NEWS: Chatbots.org survey on 3000 US and UK consumers shows it is time for chatbot integration in customer service!read more..

Dynamic Variables in ChatScript
 
 

Does anyone know how to define dynamic variables in ChatScript? I have a Loop () statement and want to initialize and populate a variable during each loop iteration with the loop iteration…e.g. during the first loop set $$loop1 = AA, second loop set $$loop2 = BB, third loop set $$loop3 = CC, X loop set $$loopX = DD, etc. The loop count will be a variable # of iterations. 

I’m looking for something within the loop statement like say on the 10th loop set $$loop($$loopnum) = “XX” which would create $$loop10 and set it to “XX.”  Or can this be done through an array?

 

 
  [ # 1 ]

the minimalist answer is you can create a name of a variable using ^join like:
$$tmp = ^join ($$loop $$counter)
and you can store a value on it indirectly via
^$$tmp = some value

One wonders how you intend to use or access these variables… do you know in advance the loop count numbers so later you will ask for the value of $$loop6 because you know it will get created? 

“Arrays” dont exist, they are represented as facts and the possibility of querying to get facts into a fact set. Less minimalist answer corresponds to merely creating a fact in the loop like
^createfact($$counter iterationvalue “some value” FACTTRANSIENT )
which will create a series of facts and you can query for all of them or some particular one of them

 

 
  [ # 2 ]

That’s exactly what I was looking for.  Thank you Bruce! And to answer your question, once the loop completes I will have the loop count so I can re-access those temp variables for further processing.

 

 
  [ # 3 ]

Whoops - I think I spoke too soon. Just tried that code out and it bombs for me when attempting to assign a value to the newly created variable. I made a simple sample that I can’t get to work:

Topic: ~trythis Keep Repeat nostay xxxyyy )
u: ( xxxyyy )
$
$loop 1
$$tmp = ^join($$xyz $$loop)
^$
$tmp hello
$$xyz1 

Any ideas?

 

 
  [ # 4 ]

Your join has a problem in that you ask for it to query 2 variables.

$$tmp = ^join(”$$xyz” $$loop)

would be more effective

 

 
  [ # 5 ]

That was it and now it works perfectly Bruce. Thank you so much for your help.

 

 
  login or register to react