Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove duplicates in a comma delimited string. 1

Status
Not open for further replies.

RustyAfro

Programmer
Jan 12, 2005
332
US
I have a string that looks like this:

255,255,124,532,322,322,322,664,342

I need it to look like this (No Duplicates)

255,124,532,322,664,342

I wrote this code, but it just returns a bunch of ",,,,,,,,,,,". The max amount of elements is 30 in the original string, but I'm not sure if I am using redim properly. I'm also not sure why the Rev_Array_Final is just filled with comma's and not the values. Any tips?
Code:
whilereadingrecords;
stringvar array Rev_Array := split({UWVW_BKB437_REV_CD_COV.REV_CD_ALL}, ",");
stringvar array Rev_Array_Final; //Scrubbed of Dups!
stringvar Rev_Codes;
numbervar Counter;
numbervar i:=1;

redim Rev_Array_Final [30];

for counter := 1 to ubound(Rev_Array) Do
(
    (
        if NOT (Rev_Array[counter] in Rev_Array_Final) then Rev_Array_Final[i]:=Rev_Codes[counter];
        i:=i+1;
    )
);

Rev_Codes:=JOIN(Rev_Array_Final,",");

Rev_Codes
 
Arggh, dontcha hate it when you post code and then see a typo? "Rev_Codes[counter];" should be "Rev_Array[counter];"

I think I almost got it now, It's returning this string:
250,,260,,270,272,,,,278,301,,,,,,,,,,,,,,,,,,,

I thought the i:=i+1 was only occurring after the "THEN clause, but it seems to be happening in every DO loop for some reason. I also will need to remove the excess ",".

There is probably a cleaner way to do this so any tips would help!

Here is code without typo
Code:
whilereadingrecords;
stringvar array Rev_Array := split({UWVW_BKB437_REV_CD_COV.REV_CD_ALL}, ",");
stringvar array Rev_Array_Final; //Scrubbed of Dups!
stringvar Rev_Codes;
numbervar Counter;
numbervar i:=1;

redim Rev_Array_Final [30];

for counter := 1 to ubound(Rev_Array) Do
(
    (
        if NOT (Rev_Array[counter] in Rev_Array_Final) then Rev_Array_Final[i]:=Rev_Array[counter];
        i:=i+1;
    )
);

Rev_Codes:=JOIN(Rev_Array_Final,",");

Rev_Codes
 
Try:

whilereadingrecords;
stringvar array Rev_Array := split({UWVW_BKB437_REV_CD_COV.REV_CD_ALL},",");
stringvar array Rev_Array_Final;
stringvar Rev_Codes;
numbervar Counter;
numbervar i;

for counter := 1 to ubound(Rev_Array) Do (
if NOT (Rev_Array[counter] in Rev_Array_Final) then (
redim preserve Rev_Array_Final [ubound(Rev_Array_Final)+1];
i := i + 1;
Rev_Array_Final := Rev_Array[counter];
Rev_Codes:=JOIN(Rev_Array_Final,",");
));
Rev_Codes

-LB
 
Ok, I have a working solution below. I want to declare the size of Rev_Array_Final in the declaration but get an error when i do so (forcing me to redim it). Other than that it seems to work ok though I am open to suggestions for better performance. Thanks everyone ;-)

Code:
whilereadingrecords;
stringvar array Rev_Array := split({UWVW_BKB437_REV_CD_COV.REV_CD_ALL}, ",");
stringvar array Rev_Array_Final; //Scrubbed of Dups!
stringvar Rev_Codes;
numbervar Counter;
numbervar Match_Count:=1;

redim Rev_Array_Final [ubound(Rev_Array)];

for counter := 1 to ubound(Rev_Array) Do
(
    (
        if NOT (Rev_Array[counter] in Rev_Array_Final) then 
            (Rev_Array_Final[Match_Count]:=Rev_Array[counter];
             Match_Count:=Match_Count+1);       //Only increment Match_Count by 1 when a new value is added to final array
    )
);

redim preserve Rev_Array_Final [Match_Count];   //Remove the extra array elements not needed
Rev_Codes:=JOIN(Rev_Array_Final,",");           //Create a new comma delimited string
Rev_Codes:=LEFT(Rev_Codes,LENGTH(Rev_Codes)-1); //Remove extra ","

Rev_Codes
 
LBASS, thanks for your tip, just saw it. Trying the redim preserve in the loop as you have it.

Thanks again!

Joel Seguin
Health Net
IT Health Economist
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top