473,396 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Remove similar numbers in array

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
6 1757
"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
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
"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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Adam | last post by:
Hi, I am trying to mark consective numbers in a data set and get a count as to how many consecutive numbers exist in the a given line of data. Here is an example line: 3, 5, 7, 9, 10, 13,...
2
by: Patrick G. | last post by:
Greetings all: ASP VB, SQL Svr 2000 I am pulling data from 3 tables. table1 holds item details table2 holds publication types and the item id from table1 table3 holds category types and...
12
by: Sam Collett | last post by:
How do I remove an item with a specified value from an array? i.e. array values 1,2,2,5,7,12,15,21 remove 2 from array would return 1,5,7,12,15,21 (12 and 21 are NOT removed, duplicates are...
4
by: Tony WONG | last post by:
i have a number of forms with fax numbers to come up into arrays and then combine to string. after that i design the flow 1. break the string to array now the string looks like this...
19
by: brasilino | last post by:
Hi Folks: I've been looking (aka googling) around with no success. I need a usability beyond 'pop()' method when removing an Array elements. For example: oName = new...
8
by: Paul | last post by:
Hi, My VB is very rusty I'm afraid! What would be the most efficient way to remove the following ASCII characters from a string? à è ì ò ù À È Ì Ò Ù á é í ó ú ý Á É Í Ó Ú Ý â ê î ô û Â Ê Î Ô...
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
3
by: joe | last post by:
hello I have a string that has values in quotes 'hello' 'here' '199' 'something else' I need to remove the quotes only when they are around numbers. To make the string lookg like 'hello' 'here'...
6
jlandbw04
by: jlandbw04 | last post by:
Okay. Here's the deal. I have this assignment for college that has me completely puzzled. I need this assignment to do the following: 1. input 12 integers into an array from the user. 2. output...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.