Connecting Tech Pros Worldwide Help | Site Map

sort out duplicated fax numbers

 
LinkBack Thread Tools Search this Thread
  #1  
Old May 10th, 2006, 05:05 AM
Tony WONG
Guest
 
Posts: n/a
Default sort out duplicated fax numbers

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 12345678,23456789,34567890...
2. check record-2 again record-1,
check record-3 again record-2 & record-1
check record-4 again record-3 & record-2 & record-1
and so on... (if duplicated, drop it)

problem - step 1 break the string to array
var CDEF = new Array(faxnumbers);
alert(CDEF[0],

it returns all the numbers (12345678,23456789,34567890...

i just wish to have 12345678

anything wrong with my script?

Grateful if you could kindly advise whether step 2 is the best way to sort
out duplicated numbers?

Thanks a lot

tony



  #2  
Old May 10th, 2006, 07:35 AM
RobG
Guest
 
Posts: n/a
Default Re: sort out duplicated fax numbers

Tony WONG wrote:[color=blue]
> 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 12345678,23456789,34567890...
> 2. check record-2 again record-1,
> check record-3 again record-2 & record-1
> check record-4 again record-3 & record-2 & record-1
> and so on... (if duplicated, drop it)
>
> problem - step 1 break the string to array
> var CDEF = new Array(faxnumbers);
> alert(CDEF[0],
>
> it returns all the numbers (12345678,23456789,34567890...
>
> i just wish to have 12345678
>
> anything wrong with my script?[/color]

Yes, it's very inefficient. If you start with a comma-delimited set of
numbers, you can split it into an array using split:

var faxNumbers = '1234,2345,3456,45646,...';
var faxNumArray = faxNumbers.split(',');

Now to get unique numbers, sort the array and load only new ones into a
new array (the numbers are sorted alphabetically, but since they are all
numbers that shouldn't matter here):

faxNumArray.sort();
var faxNumUnique = [faxNumArray[0]];
for (var i=1, j=0, len=faxNumArray.length; i<len; ++i){
if (faxNumArray[i] != faxNumUnique[j]){
faxNumUnique[++j] = faxNumArray[i];
}
}
// faxNumUnique now contains a sorted, unique list of fax numbers

[color=blue]
> Grateful if you could kindly advise whether step 2 is the best way to sort
> out duplicated numbers?[/color]

No, it's not. The best way would be to use a UNIX sort unique, but
since this is JavaScript ... see above.


--
Rob
Group FAQ: <URL:http://www.jibbering.com/faq/>
  #3  
Old May 10th, 2006, 11:55 AM
ASM
Guest
 
Posts: n/a
Default Re: sort out duplicated fax numbers

Tony WONG a écrit :[color=blue]
> 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 12345678,23456789,34567890...
> 2. check record-2 again record-1,
> check record-3 again record-2 & record-1
> check record-4 again record-3 & record-2 & record-1
> and so on... (if duplicated, drop it)
>
> problem - step 1 break the string to array
> var CDEF = new Array(faxnumbers);[/color]

CDEF = faxnumbers.split(',');
[color=blue]
> alert(CDEF[0],[/color]

would be 12345678



--
Stephane Moriaux et son [moins] vieux Mac
  #4  
Old May 11th, 2006, 01:55 AM
Tony WONG
Guest
 
Posts: n/a
Default Re: sort out duplicated fax numbers

Thanks a lot.


"RobG" <rgqld@iinet.net.au>
???????:YHg8g.1218$Nv4.171223@news.optus.net.au...[color=blue]
> Tony WONG wrote:[color=green]
>> 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 12345678,23456789,34567890...
>> 2. check record-2 again record-1,
>> check record-3 again record-2 & record-1
>> check record-4 again record-3 & record-2 & record-1
>> and so on... (if duplicated, drop it)
>>
>> problem - step 1 break the string to array
>> var CDEF = new Array(faxnumbers);
>> alert(CDEF[0],
>>
>> it returns all the numbers (12345678,23456789,34567890...
>>
>> i just wish to have 12345678
>>
>> anything wrong with my script?[/color]
>
> Yes, it's very inefficient. If you start with a comma-delimited set of
> numbers, you can split it into an array using split:
>
> var faxNumbers = '1234,2345,3456,45646,...';
> var faxNumArray = faxNumbers.split(',');
>
> Now to get unique numbers, sort the array and load only new ones into a
> new array (the numbers are sorted alphabetically, but since they are all
> numbers that shouldn't matter here):
>
> faxNumArray.sort();
> var faxNumUnique = [faxNumArray[0]];
> for (var i=1, j=0, len=faxNumArray.length; i<len; ++i){
> if (faxNumArray[i] != faxNumUnique[j]){
> faxNumUnique[++j] = faxNumArray[i];
> }
> }
> // faxNumUnique now contains a sorted, unique list of fax numbers
>
>[color=green]
>> Grateful if you could kindly advise whether step 2 is the best way to
>> sort out duplicated numbers?[/color]
>
> No, it's not. The best way would be to use a UNIX sort unique, but since
> this is JavaScript ... see above.
>
>
> --
> Rob
> Group FAQ: <URL:http://www.jibbering.com/faq/>[/color]


  #5  
Old May 11th, 2006, 05:25 PM
Dr John Stockton
Guest
 
Posts: n/a
Default Re: sort out duplicated fax numbers

JRS: In article <YHg8g.1218$Nv4.171223@news.optus.net.au>, dated Wed,
10 May 2006 07:34:16 remote, seen in news:comp.lang.javascript, RobG
<rgqld@iinet.net.au> posted :[color=blue]
>Yes, it's very inefficient. If you start with a comma-delimited set of
>numbers, you can split it into an array using split:
>
> var faxNumbers = '1234,2345,3456,45646,...';
> var faxNumArray = faxNumbers.split(',');
>
>Now to get unique numbers, sort the array and load only new ones into a
>new array (the numbers are sorted alphabetically, but since they are all
>numbers that shouldn't matter here):[/color]

In sorting to determine duplicates, the sort order does not matter, and
hence there is no relevance in their being numbers. The only need is
that identical values sort to adjacent and that comparison of a given
pair is consistent.

The obvious javascript way is to do the built-in sort, then to make a
single pass removing repeats or copying non-repeats.

If the number of duplicates is large, however, it may be better to code
the sort explicitly using a reasonably efficient algorithm, but to then
modify it so that when two items are found to be equal one of them is
dropped.

OTOH, perhaps the most efficient sorts are non-stable (i.e. items
testing as identical do not have their order preserved) and do not use
comparison-for-equality.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.