April 27, 20214 yr I'm trying to get the first occurrence of each unique value from a returned separated list using the While function. Can anyone tell me why this calculation doesn't work? SetRecursion ( While ( [ ~values = "Apple¶Apple¶Apple¶Orange¶Orange¶Orange" ; ~prevValue = "" ; ~result = "" ] ; not IsEmpty ( ~values ) ; [ ~value = GetValue ( ~values ; 1 ) ; ~result = If ( ~value ≠ ~prevValue ; ~result & "¶" & ~value ) ; ~prevValue = ~value ; ~values = RightValues ( ~values ; ValueCount ( ~values ) - 1 ) ] ; ~result ) ; 10 ) I have been staring at this all morning and I just can't see where I'm going wrong. Any help would be appreciated Thanks Edited April 27, 20214 yr by Ron Cates
April 27, 20214 yr It doesn't work because every time the value repeats, you empty the result. You can verify this by testing with: ~values = "Apple¶Apple¶Apple¶Orange¶Orange¶Orange¶Plum" ; Instead of: ~result = If ( ~value ≠ ~prevValue ; ~result & "¶" & ~value ) ; you should be doing: ~result = If ( ~value ≠ ~prevValue ; ~result & "¶" & ~value ; ~result ) ; or preferably, to avoid a leading carriage return: ~result = If ( ~value ≠ ~prevValue ; List ( ~result ; ~value ) ; ~result ) ; Edited April 27, 20214 yr by comment
April 27, 20214 yr Author Ugh! I knew it had to be right in front of me. Thank you so much. Probably staring at it too long and just not seeing my own nose in front of my face. Thank You
April 27, 20214 yr P.S. It should be noted that this does not "get the first occurrence of each unique value from a returned separated list". It returns a single value for each consecutive run of identical values. If the initial list is: Apple Apple Apple Orange Orange Orange Apple Apple Orange then the result will be: Apple Orange Apple Orange Not sure why this would be needed.
April 27, 20214 yr Author It was a starting point. I realized what you just pointed out and adjusted ~prevValue to build a returned separated list of each value that had already been accounted for and used PattenCount to determine if each line contained a previously listed value. I was able to get the result I was looking for which was just the first occurrence of each value. Of course my use case is more complicated than Apple, Apple Apple. I just simplified the While calculation to define my immediate problem. Thanks as always for all your help.
April 27, 20214 yr 4 minutes ago, Ron Cates said: adjusted ~prevValue to build a returned separated list of each value that had already been accounted for and used PattenCount to determine if each line contained a previously listed value 1. You should not use PatternCount() to determine the presence of a value in a list; FilterValues() is both simpler and faster. 2. How is this different from the built-in UniqueValues() function?
April 27, 20214 yr Author The actual text is more like Apple | Some text Apple | Some more text Apple | Different text Orange | Random text Orange | Still more text Orange | A Different text I was looking to get Apple | Some text Orange | Random text I ended up with this: SetRecursion ( While ( [ ~values = "Apple | Some text¶Apple | Some more text¶Apple | Different text¶Orange | Random text¶Orange | Still more text¶Orange | A Different text" ; ~prevValue = "" ; ~result = "" ] ; not IsEmpty ( ~values ) ; [ ~string = GetValue ( ~values ; 1 ) ; ~value = GetValue ( Substitute ( ~string ; " | " ; "¶" ) ; 1 ) ; ~result = If ( not PatternCount ( ~prevValue ; ~value & "|" ) ; List ( ~result ; ~string ) ; ~result ) ; ~prevValue = ~prevValue & ~value & "|" ; ~values = RightValues ( ~values ; ValueCount ( ~values ) - 1 ) ] ; ~result ) ; 10 ) Edited April 27, 20214 yr by Ron Cates
April 27, 20214 yr I see. So what is your expected result given a list like: Fired | One Fired | Two Fired | Three Red | Four Red | Five
April 27, 20214 yr Author Fired | One Red | Four And when I test it I see the issue. Fired and red, both contain "red". I made some adjustments While ( [ ~values = "Fired | One¶Fired | Two¶Fired | Three¶Red | Four¶Red | Five" ; ~prevValue = "|" ; ~result = "" ] ; not IsEmpty ( ~values ) ; [ ~string = GetValue ( ~values ; 1 ) ; ~value = GetValue ( Substitute ( ~string ; " | " ; "¶" ) ; 1 ) ; ~result = If ( not PatternCount ( ~prevValue ; "|" &~value & "|" ) ; List ( ~result ; ~string ) ; ~result ) ; ~prevValue = ~prevValue & ~value & "|" ; ~values = RightValues ( ~values ; ValueCount ( ~values ) - 1 ) ] ; ~result )
April 28, 20214 yr [shrug] 8 hours ago, comment said: You should not use PatternCount() to determine the presence of a value in a list; FilterValues() is both simpler and faster. -- BTW, you haven't given us a clue what this is really about. It seems like the real issue here is lack of separation between two atomic values at the origin - IOW, breaking the "one fact per field" rule - and this elaborate processing is required only as a result.
April 28, 20214 yr Author Sorry I didn't clarify. It's an interface project. I was working on developing a dynamic menu and navigation system that is driven from the list of layouts and determined by the layout naming convention. The goal was to be able to add layouts without having to update any menus, buttons or navigation scripts. Initially it was just a single menu for small projects but I went a little nuts and developed it out to be a three level navigation system. The whole thing works entirely in the Button Bar calculations. No scripting involved. It worked out quite nicely. Thanks again
Create an account or sign in to comment