input TEXT fields: 'zero' vs 'empty String'

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
Holladerwaldelf
Cave Troll
Posts: 74
Joined: Fri Aug 07, 2015 1:16 pm

input TEXT fields: 'zero' vs 'empty String'

Post by Holladerwaldelf »

I am building an input with TEXT-fields for several variables. Some variables are supposed to hold integer values, other Strings. In the input TEXT field I do not see a difference when the variable has the value '0' or when it is am empty String "". In both cases a '0' is shown. Is there a way to show the difference between '0' and 'empty String'?

Code: Select all

[h: object = json.set("","KEY1","Test","KEY2",0,"KEY3","") ]
[h: keysArray = json.append("[]","KEY1","KEY2","KEY3")]
[h: inputType = "TEXT" ]
	[h: inputStrList = ""]
	[h: prompt = ""]
	[h: options = ""]
	[h, foreach(key,keysArray), code: 
		{ 
		[value = json.get(object,key) ]
		[inputStrList = listAppend(inputStrList,strformat(" %{key} | %{value} | %{prompt} | %{inputType} | %{options}"),"##") ]
		}]	

[h: status = input(inputStrList)]
	[h: abort(status)]
PS: In the wiki there is an article showing "aliasmask's no-zero-trick" for PROPS (http://www.lmwcs.com/rptools/wiki/input#PROPS) but I do not see at the moment how to apply that to my problem.

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: input TEXT fields: 'zero' vs 'empty String'

Post by aliasmask »

Code: Select all

<!-- assumed input in this style -->
[H: local.object = json.set("{}","KEY1","Test","KEY2",0,"KEY3","") ]
<!-- none of the variable values can have ;|%$ characters and variables that dont match local variables -->
[H: local.strProp = json.toStrProp(local.object)]
<!-- sets all variables in string prop to local variable using input value -->
[H: abort(input(strformat("input.strProp|%{local.strProp}|Update Variables|PROPS|SPAN=TRUE SETVARS=UNSUFFIXED")))]
<!-- get locally set variables and put in to strprop. This needs to be done on a separate line for proper conversion -->
[H: return.strProp = trim(strPropFromVars(json.fields(local.object),"UNSUFFIXED"))]
<!-- fix for having an extra ; at the end -->
[H, if(endsWith(return.strProp,";")): return.strProp = substring(return.strProp,0,length(return.strProp))]
<!-- convert strprop to json object -->
[H: return.object = json.fromStrProp(return.strProp)]
<!-- generic output for test -->
<pre>[R: json.indent(replace(return.object,"<","<"))]</pre>
<pre>[R: json.indent(replace(local.object,"<","<"))]</pre> 
This has been tested.

User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

Re: input TEXT fields: 'zero' vs 'empty String'

Post by wolph42 »

you've encountered a limitation in MT. The only thing that you can do is replace the "" with a string that you identify as 'empty', e.g. "emptyField", or "<nothing>". Not ideal but at least then you know the difference. You code would then look like

Code: Select all

[h: object = json.set("","KEY1","Test","KEY2",0,"KEY3","") ]
[h: object    = replace(object, "", "emptyField")]
[h: keysArray = json.append("[]","KEY1","KEY2","KEY3")]
[h: inputType = "TEXT" ]
   [h: inputStrList = ""]
   [h: prompt = ""]
   [h: options = ""]
   [h, foreach(key,keysArray), code: 
      { 
      [value = json.get(object,key) ]
      [inputStrList = listAppend(inputStrList,strformat(" %{key} | %{value} | %{prompt} | %{inputType} | %{options}"),"##") ]
      }]   
[h: abort(input(inputStrList))]
   
 (....)
[h:endResult = replace(result, "emptyField", "")] 
not ideal, but a workable solution.

edit: and as usual AM is faster with an answer...

Holladerwaldelf
Cave Troll
Posts: 74
Joined: Fri Aug 07, 2015 1:16 pm

Re: input TEXT fields: 'zero' vs 'empty String'

Post by Holladerwaldelf »

Thanks! Both answers are very helpful.

Maybe it is worth considering putting them in the wiki?

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: input TEXT fields: 'zero' vs 'empty String'

Post by aliasmask »

That was my short answer. You can accomplish the same thing without the limitation of the PROPS method by creating your own form, but that requires a little more work. wolph has a link in his signature if you want to explore your own form input.


Holladerwaldelf
Cave Troll
Posts: 74
Joined: Fri Aug 07, 2015 1:16 pm

Re: input TEXT fields: 'zero' vs 'empty String'

Post by Holladerwaldelf »

I deliberately did not want to use html forms cos I thought the input dialog would be faster. Is that not so? Considering your "work-around-trick" with PROPS maybe the html-form will not be slower in this case?

PS: I noticed I am a "cave troll" now. :D How does one become a "cave troll"? Is that better than "kobold"? :)

@wolph: Unfortunately replace() seems to only work with Strings, when I try it on an JSON object (as suggested):

Code: Select all

[h: object = json.set("","KEY1","Test","KEY2",0,"KEY3","") ]
[h: object    = replace(object, "", "emptyField")]

[r: json.indent(object)]
The output is something like:
["emptyField{emptyField\"emptyFieldKemptyFieldEemptyFieldYemptyField1emptyField\"emptyField:emptyField\"emptyFieldTemptyFieldeemptyFieldsemptyFieldtemptyField\"emptyField,emptyField\"emptyFieldKemptyFieldEemptyFieldYemptyField2emptyField\"emptyField:emptyField0emptyField,emptyField\"emptyFieldKemptyFieldEemptyFieldYemptyField3emptyField\"emptyField:emptyField\"emptyField\"emptyField}emptyField"]

Holladerwaldelf
Cave Troll
Posts: 74
Joined: Fri Aug 07, 2015 1:16 pm

Re: input TEXT fields: 'zero' vs 'empty String'

Post by Holladerwaldelf »

I have three more questions to aliasmask's code:

Code: Select all

<!-- assumed input in this style -->
[H: local.object = json.set("{}","KEY1","Test","KEY2",0,"KEY3","") ]
<!-- none of the variable values can have ;|%$ characters and variables that dont match local variables -->
[H: local.strProp = json.toStrProp(local.object)]
<!-- sets all variables in string prop to local variable using input value -->
[H: abort(input(strformat("input.strProp|%{local.strProp}|Update Variables|PROPS|SPAN=TRUE SETVARS=UNSUFFIXED")))]
<!-- get locally set variables and put in to strprop. This needs to be done on a separate line for proper conversion -->

1/2 -->  [H: return.strProp = trim(strPropFromVars(json.fields(local.object),"UNSUFFIXED"))]

<!-- fix for having an extra ; at the end -->

3 --> [H, if(endsWith(return.strProp,";")): return.strProp = substring(return.strProp,0,length(return.strProp))]

<!-- convert strprop to json object -->
[H: return.object = json.fromStrProp(return.strProp)]
<!-- generic output for test -->
[R: json.indent(replace(return.object,"<","<"))] <br>
[R: json.indent(replace(local.object,"<","<"))] <br> 
1. Why not use 'input.strProp' directly? Does input() not already return a StrPropList with the correct values as one converted the json.object to the local.strProp used in the input()?

2. Why does one need to use trim()?

3. What is the advantage of using substring() instead of:

Code: Select all

return.strProp = deleteStrProp(return.strProp,"")

User avatar
aliasmask
RPTools Team
Posts: 9024
Joined: Tue Nov 10, 2009 6:11 pm
Location: Bay Area

Re: input TEXT fields: 'zero' vs 'empty String'

Post by aliasmask »

Holladerwaldelf wrote:I have three more questions to aliasmask's code:

Code: Select all

<!-- assumed input in this style -->
[H: local.object = json.set("{}","KEY1","Test","KEY2",0,"KEY3","") ]
<!-- none of the variable values can have ;|%$ characters and variables that dont match local variables -->
[H: local.strProp = json.toStrProp(local.object)]
<!-- sets all variables in string prop to local variable using input value -->
[H: abort(input(strformat("input.strProp|%{local.strProp}|Update Variables|PROPS|SPAN=TRUE SETVARS=UNSUFFIXED")))]
<!-- get locally set variables and put in to strprop. This needs to be done on a separate line for proper conversion -->

1/2 -->  [H: return.strProp = trim(strPropFromVars(json.fields(local.object),"UNSUFFIXED"))]

<!-- fix for having an extra ; at the end -->

3 --> [H, if(endsWith(return.strProp,";")): return.strProp = substring(return.strProp,0,length(return.strProp))]

<!-- convert strprop to json object -->
[H: return.object = json.fromStrProp(return.strProp)]
<!-- generic output for test -->
[R: json.indent(replace(return.object,"<","<"))] <br>
[R: json.indent(replace(local.object,"<","<"))] <br> 
1. Why not use 'input.strProp' directly? Does input() not already return a StrPropList with the correct values as one converted the json.object to the local.strProp used in the input()?

2. Why does one need to use trim()?

3. What is the advantage of using substring() instead of:

Code: Select all

return.strProp = deleteStrProp(return.strProp,"")
1. input.strProp is just a variable name and in this case isn't used because of the SETVARS=UNSUFFIXED sets the variables directly. But input.strProp holds all the input in a string prop locally. I use input. in the variable name as a prefix to avoid duplicate variable names generated by the function.

2. trim is needed because of an update that adds "; " to the end of a string prop. So, I use trim to remove the space so I can check for ; at the end. I could have left trim off and checked for "; " instead and removed 2 characters at the end.

3. You could use deleteStrProp(return.strProp,"") and you would check for an empty key value instead of checking the end for an extra ;. This may actually be a more elegant solution. There are many ways to write the same thing when coding. It just boils down to train of thought and coding style.

Post Reply

Return to “Macros”