By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,209 Members | 1,706 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,209 IT Pros & Developers. It's quick & easy.

Remove similar numbers in array

P: n/a
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 ??

Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Rimuen" <po**@pricom.no> 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)
Sl********@bls.gov
Jul 22 '05 #2

P: n/a
Tim Slattery wrote on 01 dec 2004 in
microsoft.public.inetserver.asp.general:
"Rimuen" <po**@pricom.no> 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[i]] = a[i]
}

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

r = r.substr(0,r.length-1)

Jul 22 '05 #3

P: n/a
"Rimuen" <po**@pricom.no> wrote in message
news:qC********************@news2.e.nsc.no...
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>
Jul 22 '05 #4

P: n/a
ljb
"Chris Hohmann" <no****@thankyou.com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
"Rimuen" <po**@pricom.no> wrote in message
news:qC********************@news2.e.nsc.no...
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
Jul 22 '05 #5

P: n/a
"ljb" <.> wrote in message news:OS**************@TK2MSFTNGP12.phx.gbl...
"Chris Hohmann" <no****@thankyou.com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
"Rimuen" <po**@pricom.no> wrote in message
news:qC********************@news2.e.nsc.no...
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
Jul 22 '05 #6

P: n/a
ljb

"Chris Hohmann" <no****@thankyou.com> wrote in message
news:uW**************@TK2MSFTNGP11.phx.gbl...
"ljb" <.> wrote in message news:OS**************@TK2MSFTNGP12.phx.gbl...
"Chris Hohmann" <no****@thankyou.com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
"Rimuen" <po**@pricom.no> wrote in message
news:qC********************@news2.e.nsc.no...
> 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
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.