Page 1 of 1

Howto code a mapping in a mapping

Posted: Fri Jun 17, 2011 2:45 pm
by kdroog4pvss
I was reviewing a part of PVSS script code and found the following

Code: Select all

main()
{
  mapping p;
  foo(p["xxx"]);
  DebugN(p);
}

foo(mapping& x )
{
  x["is"] = "impliciet";
}
I am wondering if this is a feature or working by accident?

Kind regards,
Kees

Re: Howto code a mapping in a mapping

Posted: Mon Jun 20, 2011 10:32 am
by mkoller
This is wrong code and does not work at all. It gives an error like:
PVSS00ui (0), 2011.06.20 10:30:04.602, IMPL, WARNING, 50, Default branch called, MappingVar, operator=, cannot assign variable of type TEXT_VAR

Where did you find this ?

Re: Howto code a mapping in a mapping

Posted: Mon Jun 20, 2011 10:42 am
by kdroog4pvss
Hi Martin,

I didn't find it, it is actually used! (I mean it is currently used in our application i was reviewing.)

I am running PVSS 3.9 patched upto P88.

I only get the error message when i use the following code, which assigns a empty string to p["xxx"] instead of a mapping:

Code: Select all

main()
{
  mapping p;
 p["xxx"] = "";
  foo(p["xxx"]);
  DebugN(p);
}

foo(mapping& x )
{
  x["is"] = "impliciet";
}
(I have discussed the code with some folks here at Croon. We feel it is not the best way the code this. The problem solved this way is the adding of a new key with a value of the type mapping.
This can also be achieved by passing the adres of the mapping and the new key as parameters to foo().

But currently the code seems to work just fine. From your reaction i get the feeling it will not do so in the future releases of PVSS. That's why i posted this on the forum.)


regards,
Kees

Re: Howto code a mapping in a mapping

Posted: Wed Jun 22, 2011 11:37 am
by mkoller
Huh ?
Your original post did include a different script than the one currently shown above ... did you change it later on ?
My reply was based on your initial code:

main()
{
mapping p;
foo(p);
DebugN(p);
}

foo(mapping& x )
{
x = "impliciet";
}

foo tries to assign a string to the complete mapping, which is not possible, therefore the error.

Your (changed) code from above now works without error
Calling foo(p["xxx"]);
Indexing a mapping with a key which does not exist yet will produce a new entry in the mapping with that key
containing an empty mixed as its value.

Re: Howto code a mapping in a mapping

Posted: Wed Jun 22, 2011 12:04 pm
by kdroog4pvss
Hi Martin,
If you look at my first code snippet it call foo with p["xxx"]. Which does not yet exists, because "xxx" is not yet used.
My second post is first creating p["xxx"] by assigning an empty string. That gives me the same error as you mentioned in your first answer.

You confirm that my first code snippet actually works.
Does this also mean it is a valid way of programming? Is it working this way by design?

Kind regards,
Kees

Re: Howto code a mapping in a mapping

Posted: Mon Jul 11, 2011 10:52 am
by Gertjan van Schijndel
Martin,

Here is another example:

Code: Select all

main() 
{
  mapping x;
  dpGet("System1:ExampleDP_SumAlert.", x["text"]);
  DebugN(x);
}
Is this code correct and allowed?
And will this code still work in future versions of wincc oa?