[Gambas-user] I need a hint on how to deleted duplicate items in a array

Fernando Cabral fernandojosecabral at ...626...
Fri Jun 30 13:20:55 CEST 2017


2017-06-30 7:44 GMT-03:00 Fabien Bodard <gambas.fr at ...626...>:

> The best way is the nando one ... at least for gambas.
>
> As you have not to matter about what is the index value or the order,
> the walk ahead option is the better.
>
>
> Then Fernando ... for big, big things... I think you need to use a DB.
> Or a native language.... maybe a sqlite memory structure can be good.
>

Fabien, since this is a one-time only thing, I don't think I'd be better
off witha database.
Basically, I read a text file an then break it down into words, sentences
and paragraphs.
Next I  count the items in each array (words, sentences paragraphs).
Array.count works wonderfully.
After that, have to eliminate the duplicate words (Array.words). But in
doing it, al also have to count
how many times each word appeared.

Finally I sort the Array.Sentences and the Array.Paragraphs by size
(string.len()). The Array.WOrds are
sorted by count + lenght. This is all woring good.

So, my quest is for the fastest way do eliminate the words duplicates while
I count them.
For the time being, here is a working solution based on system' s sort |
uniq:

Here is one of the versions I have been using:

Exec ["/usr/bin/uniq", "Unsorted.txt", "Sorted.srt2"] Wait
Exec ["/usr/bin/uniq", "-ci", "SortedWords.srt2",  SortedWords.srt3"] Wait
Exec ["/usr/bin/sort", "-bnr", SortedWords.srt3] To UniqWords

WordArray = split (UniqWords, "\n")

So, I end up with the result I want. It's effective. Now, it would be more
elegant If I could do the same
with Gambas. Of course, the sorting would be easy with the builting
WordArray.sort ().
But how about te '"/usr/bin/uniq", "-ci" ...' part?

Regards

- fernando



> >
> >
> >
> >
> >
> > 2017-06-27 15:43 GMT-03:00 Jussi Lahtinen <jussi.lahtinen at ...626...>:
> >
> >> As Fernando stated your code is good only for small arrays. But if
> someone
> >> is going to use it, here is correct implementation:
> >>
> >> For x = 0 to a.Max
> >>   if z.Find(a[x]) = -1 Then z.Add(a[x])
> >> Next
> >>
> >>
> >> z.Exist() might be faster... I don't know.
> >>
> >>
> >>
> >> Jussi
> >>
> >>
> >>
> >> On Tue, Jun 27, 2017 at 6:59 PM, <nando_f at ...951...> wrote:
> >>
> >> > Well, there is complicated, then there is simplicity:
> >> > I tested this. Works for sorted, unsorted.
> >> > Can't be any simpler.
> >> >
> >> > Public Function RemoveMultiple(a As String[]) As String[]
> >> >
> >> > Dim x as Integer
> >> > Dim z as NEW STRING[]
> >> >
> >> > For x = 1 to a.count()
> >> >   if z.Find(a) = 0 Then z.Add(a[x])
> >> > Next
> >> >
> >> > 'if you want it sorted, do it here
> >> > Return z
> >> >
> >> > END
> >> >
> >> > ' - - - - -
> >> > use it this way:
> >> >
> >> > myArray = RemoveMultiple(myArray)
> >> >   'the z array is now myArray.
> >> >   'the original array is destroyed because there are no references.
> >> >
> >> >
> >> >
> >> > --
> >> > Open WebMail Project (http://openwebmail.org)
> >> >
> >> >
> >> > ---------- Original Message -----------
> >> > From: Gianluigi <bagonergi at ...626...>
> >> > To: mailing list for gambas users <gambas-user at lists.sourceforge.net>
> >> > Sent: Tue, 27 Jun 2017 16:52:48 +0200
> >> > Subject: Re: [Gambas-user] I need a hint on how to deleted duplicate
> >> items
> >> > in a array
> >> >
> >> > > My two cents.
> >> > >
> >> > > Public Sub Main()
> >> > >
> >> > >   Dim sSort As String[] = ["A", "B", "B", "B", "C", "D", "D", "E",
> "E",
> >> > > "E", "E", "F"]
> >> > >   Dim sSame As String[] = sSort
> >> > >   Dim bb As New Byte[]
> >> > >   Dim sSingle As New String[]
> >> > >   Dim i, n As Integer
> >> > >
> >> > >   For i = 0 To sSort.Max
> >> > >     If i < sSort.Max Then
> >> > >       If sSort[i] = sSame[i + 1] Then
> >> > >         Inc n
> >> > >       Else
> >> > >         sSingle.Push(sSort[i])
> >> > >         bb.Push(n + 1)
> >> > >         n = 0
> >> > >       Endif
> >> > >     Endif
> >> > >   Next
> >> > >   sSingle.Push(sSort[sSort.Max])
> >> > >   bb.Push(n + 1)
> >> > >   For i = 0 To sSingle.Max
> >> > >     Print sSingle[i]
> >> > >   Next
> >> > >   For i = 0 To bb.Max
> >> > >     Print bb[i] & sSingle[i]
> >> > >   Next
> >> > >
> >> > > End
> >> > >
> >> > > Regards
> >> > > Gianluigi
> >> > >
> >> > > 2017-06-27 16:33 GMT+02:00 <nando_f at ...951...>:
> >> > >
> >> > > > Another very effective and simple would be:
> >> > > >
> >> > > > You have your array with data
> >> > > > You create a new empty array.
> >> > > >
> >> > > > Loop through each item in your array with data
> >> > > > If it's not in the new array, then add it.
> >> > > >
> >> > > > Destroy the original array.
> >> > > > Keep the new one.
> >> > > > ...something like (syntax may not be correct)
> >> > > >
> >> > > > Public Function RemoveMultiple(a As String[]) As String[]
> >> > > >
> >> > > >   Dim x as Integer
> >> > > >   Dim z as NEW STRING[]
> >> > > >
> >> > > >   For x = 1 to a.count()
> >> > > >     if z.Find(a) = 0 Then z.Add(a[x])
> >> > > >   Next
> >> > > >
> >> > > >   Return z
> >> > > >
> >> > > > END
> >> > > >
> >> > > > -Nando (Canada)
> >> > > >
> >> > > >
> >> > > >
> >> > > >
> >> > > > --
> >> > > > Open WebMail Project (http://openwebmail.org)
> >> > > >
> >> > > >
> >> > > > ---------- Original Message -----------
> >> > > > From: Hans Lehmann <hans at ...3219...>
> >> > > > To: gambas-user at lists.sourceforge.net
> >> > > > Sent: Tue, 27 Jun 2017 15:51:19 +0200
> >> > > > Subject: Re: [Gambas-user] I need a hint on how to deleted
> duplicate
> >> > items
> >> > > > in a array
> >> > > >
> >> > > > > Hello,
> >> > > > >
> >> > > > > look here:
> >> > > > >
> >> > > > > 8<----------------------------------------------------------
> >> > > > ---------------------
> >> > > > > ---------- Public Function RemoveMultiple(aStringListe As
> String[])
> >> > As
> >> > > > String[]
> >> > > > >  Dim iCount As Integer   Dim iIndex As Integer   Dim sElement As
> >> > String
> >> > > > >
> >> > > > >    iIndex = 0 ' Initialisierung NICHT notwendig
> >> > > > >    While iIndex < aStringListe.Count
> >> > > > >      iCount = 0
> >> > > > >      sElement = aStringListe[iIndex]
> >> > > > >      While aStringListe.Find(sElement) <> -1
> >> > > > >        Inc iCount
> >> > > > >        aStringListe.Remove(aStringListe.Find(sElement))
> >> > > > >      Wend
> >> > > > >      If iCount Mod 2 = 1 Then
> >> > > > >         aStringListe.Add(sElement, iIndex)
> >> > > > >         Inc iIndex
> >> > > > >      Endif ' iCount Mod 2 = 1 ?
> >> > > > >    Wend
> >> > > > >
> >> > > > >    Return aStringListe
> >> > > > >
> >> > > > > End ' RemoveMultiple(...)
> >> > > > > 8<----------------------------------------------------------
> >> > > > ---------------------
> >> > > > > ----------
> >> > > > >
> >> > > > > Hans
> >> > > > > gambas-buch.de
> >> > > > >
> >> > > > > ------------------------------------------------------------
> >> > > > ------------------
> >> > > > > Check out the vibrant tech community on one of the world's most
> >> > > > > engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> >> > > > > _______________________________________________
> >> > > > > Gambas-user mailing list
> >> > > > > Gambas-user at lists.sourceforge.net
> >> > > > > https://lists.sourceforge.net/lists/listinfo/gambas-user
> >> > > > ------- End of Original Message -------
> >> > > >
> >> > > >
> >> > > > ------------------------------------------------------------
> >> > > > ------------------
> >> > > > Check out the vibrant tech community on one of the world's most
> >> > > > engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> >> > > > _______________________________________________
> >> > > > Gambas-user mailing list
> >> > > > Gambas-user at lists.sourceforge.net
> >> > > > https://lists.sourceforge.net/lists/listinfo/gambas-user
> >> > > >
> >> > > ------------------------------------------------------------
> >> > ------------------
> >> > > Check out the vibrant tech community on one of the world's most
> >> > > engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> >> > > _______________________________________________
> >> > > Gambas-user mailing list
> >> > > Gambas-user at lists.sourceforge.net
> >> > > https://lists.sourceforge.net/lists/listinfo/gambas-user
> >> > ------- End of Original Message -------
> >> >
> >> >
> >> > ------------------------------------------------------------
> >> > ------------------
> >> > Check out the vibrant tech community on one of the world's most
> >> > engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> >> > _______________________________________________
> >> > Gambas-user mailing list
> >> > Gambas-user at lists.sourceforge.net
> >> > https://lists.sourceforge.net/lists/listinfo/gambas-user
> >> >
> >> ------------------------------------------------------------
> >> ------------------
> >> Check out the vibrant tech community on one of the world's most
> >> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> >> _______________________________________________
> >> Gambas-user mailing list
> >> Gambas-user at lists.sourceforge.net
> >> https://lists.sourceforge.net/lists/listinfo/gambas-user
> >>
> >
> >
> >
> > --
> > Fernando Cabral
> > Blogue: http://fernandocabral.org
> > Twitter: http://twitter.com/fjcabral
> > e-mail: fernandojosecabral at ...626...
> > Facebook: f at ...3654...
> > Telegram: +55 (37) 99988-8868
> > Wickr ID: fernandocabral
> > WhatsApp: +55 (37) 99988-8868
> > Skype:  fernandojosecabral
> > Telefone fixo: +55 (37) 3521-2183
> > Telefone celular: +55 (37) 99988-8868
> >
> > Enquanto houver no mundo uma só pessoa sem casa ou sem alimentos,
> > nenhum político ou cientista poderá se gabar de nada.
> > ------------------------------------------------------------
> ------------------
> > Check out the vibrant tech community on one of the world's most
> > engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> > _______________________________________________
> > Gambas-user mailing list
> > Gambas-user at lists.sourceforge.net
> > https://lists.sourceforge.net/lists/listinfo/gambas-user
>
>
>
> --
> Fabien Bodard
>



-- 
Fernando Cabral
Blogue: http://fernandocabral.org
Twitter: http://twitter.com/fjcabral
e-mail: fernandojosecabral at ...626...
Facebook: f at ...3654...
Telegram: +55 (37) 99988-8868
Wickr ID: fernandocabral
WhatsApp: +55 (37) 99988-8868
Skype:  fernandojosecabral
Telefone fixo: +55 (37) 3521-2183
Telefone celular: +55 (37) 99988-8868

Enquanto houver no mundo uma só pessoa sem casa ou sem alimentos,
nenhum político ou cientista poderá se gabar de nada.



More information about the User mailing list