Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

First Occurrence of Unique Values In A List

Featured Replies

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 by Ron Cates

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 by comment

  • 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

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.

 

  • 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.

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?

 

  • 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 by Ron Cates

I see.

So what is your expected result given a list like:

Fired | One
Fired | Two
Fired | Three
Red | Four
Red | Five

 

  • 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

)

[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. 

 

  • 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

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.