Ruby question - Filtering array duplications

For general discussion related FlowStone
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Ruby question - Filtering array duplications

Post by kortezzzz »

Hi,

I tried few methods for this from the Ruby guide, but couldn't translate them with correct syntax in FS.

The example:
We have a given array:
dog
dog
frog
fish
cat
cat

We need to filter the duplications which are "dog" and "cat" and end up with
dog
frog
fish
cat

What would be the correct syntax for that?
User avatar
DaveyBoy
Posts: 131
Joined: Wed May 11, 2016 9:18 pm
Location: Leeds UK

Re: Ruby question - Filtering array duplications

Post by DaveyBoy »

Try array.uniq :)
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: Ruby question - Filtering array duplications

Post by kortezzzz »

Thanks Dave. I tried it but couldn't formulate it correctly. What exact syntax would you use to make it work in FS?
User avatar
DaveyBoy
Posts: 131
Joined: Wed May 11, 2016 9:18 pm
Location: Leeds UK

Re: Ruby question - Filtering array duplications

Post by DaveyBoy »

Like so:

["dog","dog","frog","fish","cat","cat"].uniq

Here's what I use for reference:
https://ruby-doc.org/core-1.9.3/Array.html#method-i-uniq

Hope this helps :)
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: Ruby question - Filtering array duplications

Post by kortezzzz »

Thanks,

But what if the array is not pre-determined (to "dog", cat", etc.)? Is there any kind of code that can recognize duplications in any given array?
User avatar
DaveyBoy
Posts: 131
Joined: Wed May 11, 2016 9:18 pm
Location: Leeds UK

Re: Ruby question - Filtering array duplications

Post by DaveyBoy »

Hmmm . . .I don't really understand what you're asking, uniq will remove any and all dupes as far as I know.

Can you be more specific in what you are trying to achieve?
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Ruby question - Filtering array duplications

Post by tulamide »

DaveyBoy is right. The solution to what you described is Array #uniq, and it doesn't matter, what exactly is in the arrays, as long as the objects are comparable.
"There lies the dog buried" (German saying translated literally)
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: Ruby question - Filtering array duplications

Post by kortezzzz »

Ok, so there is a method for this :)
But how I write it correctly inside FS Ruby module? Tried to formulate a code, but it didn't worked for me, so seems like I'm doing something wrong.
RJHollins
Posts: 1573
Joined: Thu Mar 08, 2012 7:58 pm

Re: Ruby question - Filtering array duplications

Post by RJHollins »

kortezzzz wrote:Ok, so there is a method for this :)
But how I write it correctly inside FS Ruby module? Tried to formulate a code, but it didn't worked for me, so seems like I'm doing something wrong.

Post us an example of the code, so we can see what you are trying to do.
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: Ruby question - Filtering array duplications

Post by kortezzzz »

Found how to formulate it correctrlly in FS. An example schematic is attached.

Cheers!
Attachments
(filtering array duplicates).fsm
(344 Bytes) Downloaded 873 times
Post Reply