Remove similar numbers in array

Discussion in 'ASP General' started by Rimuen, Dec 1, 2004.

  1. Rimuen

    Rimuen Guest

    Have a string contains numbers from database. But there is similar numbers
    want to remove
    Example:
    1,3,6,6,6,12,13,14,15,15,15,15
    Want to remove the similar numbers so it would be like:
    1,3,6,12,13,14,15

    Any help ??
     
    Rimuen, Dec 1, 2004
    #1
    1. Advertising

  2. Rimuen

    Tim Slattery Guest

    "Rimuen" <> wrote:

    >Have a string contains numbers from database. But there is similar numbers
    >want to remove
    >Example:
    >1,3,6,6,6,12,13,14,15,15,15,15
    >Want to remove the similar numbers so it would be like:
    >1,3,6,12,13,14,15


    In VBS: use the split function to break the string into an array on
    commas. If the array is not in order, you'd want to sort it. Then
    simply iterate through the array. If the current element matches the
    previous one, ignore it. If not, concatenate it to the result string.
    (Watch out for the first element.)

    There is a split method of the string object in JavaScript. You could
    use that to start off the same process in that language.

    --
    Tim Slattery
    MS MVP(DTS)
     
    Tim Slattery, Dec 1, 2004
    #2
    1. Advertising

  3. Rimuen

    Evertjan. Guest

    Tim Slattery wrote on 01 dec 2004 in
    microsoft.public.inetserver.asp.general:

    > "Rimuen" <> wrote:
    >
    >>Have a string contains numbers from database. But there is similar
    >>numbers want to remove
    >>Example:
    >>1,3,6,6,6,12,13,14,15,15,15,15
    >>Want to remove the similar numbers so it would be like:
    >>1,3,6,12,13,14,15

    >
    > In VBS: use the split function to break the string into an array on
    > commas. If the array is not in order, you'd want to sort it. Then
    > simply iterate through the array. If the current element matches the
    > previous one, ignore it. If not, concatenate it to the result string.
    > (Watch out for the first element.)
    >
    > There is a split method of the string object in JavaScript. You could
    > use that to start off the same process in that language.
    >


    If you do not want to sort,
    but are content with th first occurrance,
    this ASP jscript will do,
    and is much faster than sorting:


    s = "15,15,1,3,6,6,6,12,13,14,15,15"
    a = s.split(',')
    b = new Array()
    r = ""

    for (i=0;i<a.length;i++){
    b[+a] = a
    }

    for (x in b){
    r += x+','
    }

    r = r.substr(0,r.length-1)
     
    Evertjan., Dec 1, 2004
    #3
  4. "Rimuen" <> wrote in message
    news:qCprd.10584$...
    > Have a string contains numbers from database. But there is similar numbers
    > want to remove
    > Example:
    > 1,3,6,6,6,12,13,14,15,15,15,15
    > Want to remove the similar numbers so it would be like:
    > 1,3,6,12,13,14,15
    >
    > Any help ??


    <script language="JavaScript" runat="SERVER">
    Response.Write(("," +
    "1,3,6,6,6,12,13,14,15,15,15,15").replace(/(,\d+)(?=(,.*)*\1($|,))/g,"").sub
    str(1));
    </script>
     
    Chris Hohmann, Dec 2, 2004
    #4
  5. Rimuen

    ljb Guest

    "Chris Hohmann" <> wrote in message
    news:%23$...
    > "Rimuen" <> wrote in message
    > news:qCprd.10584$...
    > > Have a string contains numbers from database. But there is similar

    numbers
    > > want to remove
    > > Example:
    > > 1,3,6,6,6,12,13,14,15,15,15,15
    > > Want to remove the similar numbers so it would be like:
    > > 1,3,6,12,13,14,15
    > >
    > > Any help ??

    >
    > <script language="JavaScript" runat="SERVER">
    > Response.Write(("," +
    >

    "1,3,6,6,6,12,13,14,15,15,15,15").replace(/(,\d+)(?=(,.*)*\1($|,))/g,"").sub
    > str(1));
    > </script>
    >

    For me to better understand the script I rewrote it in vbscript. Regular
    expressions are amazing stuff!

    mylist = "1,3,6,6,6,12,13,14,15,15,15,15"
    mypattern = "(,\d+)(?=(,.*)*\1($|,))"

    set re = createobject("VBScript.RegExp")
    re.Global = true
    re.pattern = mypattern

    myresult = "Source: " & mylist & vbcrlf & _
    "Pattern: " & mypattern & vbcrlf & _
    "Result: " & re.replace(mylist,"") & vbcrlf & _
    "Duplicates: " & vbcrlf

    for each match in re.execute(mylist)
    myresult = myresult & match & vbcrlf
    next

    wscript.echo myresult
     
    ljb, Dec 7, 2004
    #5
  6. "ljb" <.> wrote in message news:...
    > "Chris Hohmann" <> wrote in message
    > news:%23$...
    > > "Rimuen" <> wrote in message
    > > news:qCprd.10584$...
    > > > Have a string contains numbers from database. But there is similar

    > numbers
    > > > want to remove
    > > > Example:
    > > > 1,3,6,6,6,12,13,14,15,15,15,15
    > > > Want to remove the similar numbers so it would be like:
    > > > 1,3,6,12,13,14,15
    > > >
    > > > Any help ??

    > >
    > > <script language="JavaScript" runat="SERVER">
    > > Response.Write(("," +
    > >

    >

    "1,3,6,6,6,12,13,14,15,15,15,15").replace(/(,\d+)(?=(,.*)*\1($|,))/g,"").sub
    > > str(1));
    > > </script>
    > >

    > For me to better understand the script I rewrote it in vbscript. Regular
    > expressions are amazing stuff!
    >
    > mylist = "1,3,6,6,6,12,13,14,15,15,15,15"
    > mypattern = "(,\d+)(?=(,.*)*\1($|,))"
    >
    > set re = createobject("VBScript.RegExp")
    > re.Global = true
    > re.pattern = mypattern
    >
    > myresult = "Source: " & mylist & vbcrlf & _
    > "Pattern: " & mypattern & vbcrlf & _
    > "Result: " & re.replace(mylist,"") & vbcrlf & _
    > "Duplicates: " & vbcrlf
    >
    > for each match in re.execute(mylist)
    > myresult = myresult & match & vbcrlf
    > next
    >
    > wscript.echo myresult


    You'll want to append a comma (,) to the beginning when performing the
    match/replace, then remove the comma when outputting. This is so the first
    number in the list can also participate in the matching. Add a "1" to the
    list somewhere in the middle to see what I'm talking about. I agree, regular
    expressions are pretty amazing.

    -Chris Hohmann
     
    Chris Hohmann, Dec 8, 2004
    #6
  7. Rimuen

    ljb Guest

    "Chris Hohmann" <> wrote in message
    news:...
    > "ljb" <.> wrote in message news:...
    > > "Chris Hohmann" <> wrote in message
    > > news:%23$...
    > > > "Rimuen" <> wrote in message
    > > > news:qCprd.10584$...
    > > > > Have a string contains numbers from database. But there is similar

    > > numbers
    > > > > want to remove
    > > > > Example:
    > > > > 1,3,6,6,6,12,13,14,15,15,15,15
    > > > > Want to remove the similar numbers so it would be like:
    > > > > 1,3,6,12,13,14,15
    > > > >
    > > > > Any help ??
    > > >
    > > > <script language="JavaScript" runat="SERVER">
    > > > Response.Write(("," +
    > > >

    > >

    >

    "1,3,6,6,6,12,13,14,15,15,15,15").replace(/(,\d+)(?=(,.*)*\1($|,))/g,"").sub
    > > > str(1));
    > > > </script>
    > > >

    > > For me to better understand the script I rewrote it in vbscript. Regular
    > > expressions are amazing stuff!
    > >
    > > mylist = "1,3,6,6,6,12,13,14,15,15,15,15"
    > > mypattern = "(,\d+)(?=(,.*)*\1($|,))"
    > >
    > > set re = createobject("VBScript.RegExp")
    > > re.Global = true
    > > re.pattern = mypattern
    > >
    > > myresult = "Source: " & mylist & vbcrlf & _
    > > "Pattern: " & mypattern & vbcrlf & _
    > > "Result: " & re.replace(mylist,"") & vbcrlf & _
    > > "Duplicates: " & vbcrlf
    > >
    > > for each match in re.execute(mylist)
    > > myresult = myresult & match & vbcrlf
    > > next
    > >
    > > wscript.echo myresult

    >
    > You'll want to append a comma (,) to the beginning when performing the
    > match/replace, then remove the comma when outputting. This is so the first
    > number in the list can also participate in the matching. Add a "1" to the
    > list somewhere in the middle to see what I'm talking about. I agree,

    regular
    > expressions are pretty amazing.
    >
    > -Chris Hohmann
    >
    >

    Yes, I was puzzled by the leading comma. I was quite sure it served some
    purpose but it didn't detect its need in my experiment.

    thanks
    LJB
     
    ljb, Dec 8, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Subra
    Replies:
    25
    Views:
    1,262
    user923005
    Mar 8, 2007
  2. Andrew Tatum

    Fibonacci Numbers and Lucas Numbers

    Andrew Tatum, May 26, 2007, in forum: C++
    Replies:
    6
    Views:
    615
    Howard
    May 27, 2007
  3. Lance Hoffmeyer
    Replies:
    2
    Views:
    551
    Lance Hoffmeyer
    Jul 26, 2007
  4. jko170
    Replies:
    9
    Views:
    140
    jko170
    Jan 21, 2009
  5. GIMME
    Replies:
    5
    Views:
    202
    Thomas 'PointedEars' Lahn
    Jul 26, 2004
Loading...

Share This Page