Remove similar numbers in array

R

Rimuen

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 ??
 
T

Tim Slattery

Rimuen said:
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.
 
E

Evertjan.

Tim Slattery wrote on 01 dec 2004 in
microsoft.public.inetserver.asp.general:
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)
 
C

Chris Hohmann

Rimuen said:
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>
 
L

ljb

Chris Hohmann said:
<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
 
C

Chris Hohmann

ljb said:
"1,3,6,6,6,12,13,14,15,15,15,15").replace(/(,\d+)(?=(,.*)*\1($|,))/g,"").sub
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
 
L

ljb

Chris Hohmann said:
"1,3,6,6,6,12,13,14,15,15,15,15").replace(/(,\d+)(?=(,.*)*\1($|,))/g,"").sub

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top