get unique values from text

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
dbcbos
Posts: 23
Joined: Sun Jun 24, 2012 9:49 am

get unique values from text

Post by dbcbos » Thu Dec 20, 2012 11:12 am

Hi all,

is there a way to get unique values from text eg:

I have the following text in a field

Code: Select all

product
product
product
service
item
item
item
item
and I like the following outcome

Code: Select all

product
service
item
I know I could loop over all lines save all unknown words/items in a Dictionary/Array and skip if it is among the lines of my dictionary array. Just wondering if there is a better way to do this?

Basically what I am trying to do is get all unique tags in an XML File.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: get unique values from text

Post by Klaus » Thu Dec 20, 2012 11:51 am

HI dbcbos,

the fastest way is to create an array like this:

Code: Select all

on mouseUp
  put fld 1 into tF
  put empty into tArray

  ## We create an ARRAY where your entries are the keys
  ## with the trick of adding something to the CONTENT of the keys, 
  ## we will end with the unique names of your entries
  repeat for each line i in tF
    add 1 to tArray[i]
  end repeat

  ## We don't need the CONTENT of the array, just the KEYS, which are now what we are looking for :-)
  put keys of tArray into fld 2
end mouseUp
"fld 1" contains your doublette strings, "fld 2" will show all entries without the doublettes:
service
product
item

Best

Klaus

dbcbos
Posts: 23
Joined: Sun Jun 24, 2012 9:49 am

Re: get unique values from text

Post by dbcbos » Thu Dec 20, 2012 11:55 am

Oh joy. That's a sly hack. Thanks.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: get unique values from text

Post by Klaus » Thu Dec 20, 2012 12:10 pm

:D

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: get unique values from text

Post by Mark » Thu Dec 20, 2012 1:15 pm

Hi,

I don't know if it executes quicker, but it is less code:

Code: Select all

put field "Your List" into myListArray
split myListArray by cr and tab
combine myListArray by cr and tab
Kind regards,

Mark
Last edited by Mark on Thu Dec 20, 2012 1:31 pm, edited 2 times in total.
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: get unique values from text

Post by Klaus » Thu Dec 20, 2012 1:22 pm

Hi Mark,

hmm, I get this result with your script:
product
product
product
service
item
item
item
item

Not really what I exspected 8)


Best

Klaus

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: get unique values from text

Post by Mark » Thu Dec 20, 2012 1:28 pm

Klaus, try again ;-)

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: get unique values from text

Post by Klaus » Thu Dec 20, 2012 1:58 pm

Same script, same result :wink:

Yep, with "and tab" it surely works...

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: get unique values from text

Post by Mark » Thu Dec 20, 2012 2:07 pm

Klaus,

Lets not confuse the others. It works correctly now, right?

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: get unique values from text

Post by Klaus » Thu Dec 20, 2012 2:19 pm

Yes! :D

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: get unique values from text

Post by Mark » Thu Dec 20, 2012 2:21 pm

Great :-)
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Post Reply