文本文档内容如下 // s1 s2 s3 s4 s5
20061217,D,B,DKDDA332021,ESA3332SS1
20061217,D,B,DKDDA332022,ESA3332SS2
20061217,D,B,DKDDA332023,ESA3332SS3
20061217,D,B,DKDDA332021,ESA3332SS2
20061217,D,B,DKDDA332025,ESA3332SS2
20061217,D,B,DKDDA332021,ESA3332SS3
20061217,D,B,DKDDA332022,ESA3332SS7其中在文本文档中,s4和s5 会有重复的数据.我想把重复的全部删除了.
但是用SORT排序,只能对整行排序.能不能按照s4和s4来进行排序,并删除其中的重复能容(整行)!谢谢大家
凡S4有相同的,只留一行,其余行删去;凡S5有相同的,只留一行
20061217,D,B,DKDDA332021,ESA3332SS1
20061217,D,B,DKDDA332022,ESA3332SS2
20061217,D,B,DKDDA332023,ESA3332SS3
20061217,D,B,DKDDA332021,ESA3332SS2
20061217,D,B,DKDDA332025,ESA3332SS2
20061217,D,B,DKDDA332021,ESA3332SS3
20061217,D,B,DKDDA332022,ESA3332SS7其中在文本文档中,s4和s5 会有重复的数据.我想把重复的全部删除了.
但是用SORT排序,只能对整行排序.能不能按照s4和s4来进行排序,并删除其中的重复能容(整行)!谢谢大家
凡S4有相同的,只留一行,其余行删去;凡S5有相同的,只留一行
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,StrUtils;type
TForm1 = class(TForm)
Button1: TButton;
OpenDialog1: TOpenDialog;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
var
newlist,s4list,filelist:tstringlist;
liushui,dates,barcode,company,inout:string;
i:integer;
s4:string;
off,d1,d2,d3,d4:integer;
begin
opendialog1.Execute;
filelist:=TStringList.create;
filelist.loadfromfile(opendialog1.FileName);
newlist:=TStringList.Create;
s4list:=TStringList.Create;
s4list.sorted:=True; //排序,将加快搜索速度for i:=0 to filelist.count -1 do
begin
off:=posEx(',',filelist[i],0);
d1:=off;
dates:=copy(filelist[i],0,d1-1);
off:=posEx(',',filelist[i],off+1);
d2:=off;
company:=copy(filelist[i],d1+1,d2-d1-1);
off:=posEx(',',filelist[i],off+1);
d3:=off;
inout:=copy(filelist[i],d2+1,d3-d2-1);
off:=posEx(',',filelist[i],off+1);
d4:=off;
barcode:=copy(filelist[i],d3+1,d4-d3-1);
off:=posEx(',',filelist[i],off+1);
liushui:= copy(filelist[i],d4+1,100);
s4:=barcode; //s4
if newlist.indexof(s4)>0 then
begin
newlist.add(filelist[i]);
s4list.add(s4);
end;
end;newlist.savetofile(opendialog1.FileName)
end;end.刚才测试并没通过~~S4重复的还存在
var
SL : TStringList;
I,K: Integer;
str: string;
begin
SL := TStringList.Create;
{ 原文本位于程序目录下的b.txt }
SL.LoadFromFile(sysutils.ExtractFilePath(application.ExeName)+'b.txt');
{以下是把S4相同的删除余一行}
for I := SL.Count-1 downto 0 do
begin
Str := SL[I];
for K := 1 to 3 do
Str := Copy(Str,pos(',',Str)+1,MAXINT);
Str := Copy(Str,1,Pos(',',Str)-1);
SL[I] := Str + ',' + SL[I];
end;
SL.Sort;
str := Copy(SL[SL.count-1],1,pos(',',SL[SL.count-1])-1);
SL[SL.count-1] := Copy(SL[SL.count-1],pos(',',SL[SL.count-1])+1,MAXINT);
for I := SL.Count-2 downto 0 do
begin
if Copy(SL[I],1,pos(',',SL[I])-1) = Str then
SL.Delete(I)
else
begin
str := Copy(SL[I],1,pos(',',SL[I])-1);
SL[I] := Copy(SL[I],pos(',',SL[I])+1,MAXINT);
end;
end;
{以下是把S5相同的删除余一行}
for I := SL.Count-1 downto 0 do
begin
Str := SL[I];
for K := 1 to 4 do
Str := Copy(Str,pos(',',Str)+1,MAXINT);
SL[I] := Str + ',' + SL[I];
end;
SL.Sort;
str := Copy(SL[SL.count-1],1,pos(',',SL[SL.count-1])-1);
SL[SL.count-1] := Copy(SL[SL.count-1],pos(',',SL[SL.count-1])+1,MAXINT);
for I := SL.Count-2 downto 0 do
begin
if Copy(SL[I],1,pos(',',SL[I])-1) = Str then
SL.Delete(I)
else
begin
str := Copy(SL[I],1,pos(',',SL[I])-1);
SL[I] := Copy(SL[I],pos(',',SL[I])+1,MAXINT);
end;
end;
{全部整理完毕,结果存在程序目录下的 bb.txt }
SL.SaveToFile(sysutils.ExtractFilePath(application.ExeName)+'bb.txt');
SL.Free;
end;
20061217,D,B,DKDDA332022,ESA3332SS2
20061217,D,B,DKDDA332023,ESA3332SS3
20061217,D,B,DKDDA332021,ESA3332SS2
20061217,D,B,DKDDA332025,ESA3332SS2
20061217,D,B,DKDDA332021,ESA3332SS3
20061217,D,B,DKDDA332022,ESA3332SS7
DKDDA332021
怎么会被删除了~~很奇怪哦,你帮我看看吧
20061217,D,B,DKDDA332022,ESA3332SS2
20061217,D,B,DKDDA332023,ESA3332SS3
20061217,D,B,DKDDA332021,ESA3332SS2
20061217,D,B,DKDDA332025,ESA3332SS2
20061217,D,B,DKDDA332021,ESA3332SS3
20061217,D,B,DKDDA332022,ESA3332SS7大哥唉,你看一下,DKDDA332021全删干净,是因为在第二次根据S5判断时,有重复的唉,很不幸唉你好好看一下.
如果要优化这个算法,很复杂了唉
20061217,D,B,DKDDA332021,ESA3332SS3
20061217,D,B,DKDDA332022,ESA3332SS7
20061217,D,B,DKDDA332023,ESA3332SS3
20061217,D,B,DKDDA332025,ESA3332SS2
然后,很不幸,20061217,D,B,DKDDA332021,ESA3332SS3这行,因为ESA3332SS3与它行有重复,所以被搞掉了
begin
Result := CompareText(TStrings(Item1)[3], TStringList(Item2)[3]);
end;function CompareS5(Item1, Item2: Pointer): Integer;
begin
Result := CompareText(TStrings(Item1)[4], TStringList(Item2)[4]);
end;procedure TFormDemo.ButtonRemoveClick(Sender: TObject);
var
StringsList: TList;
Strings: TStrings;
TmpStrings: TStrings;
Idx: Integer;
begin
if OpenDialog.Execute then
begin
Memo.Lines.LoadFromFile(OpenDialog.FileName); //把数据写入到List
StringsList := TList.Create;
for Idx := 0 to Pred(Memo.Lines.Count) do
begin
Strings := TStringList.Create;
Strings.Delimiter := ',';
Strings.DelimitedText := Memo.Lines[Idx];
StringsList.Add(Pointer(Strings));
end; TmpStrings := TStringList.Create; //删除S4相同的行
StringsList.Sort(CompareS4); //按S4排序
TmpStrings.Assign(TStrings(StringsList.Items[Pred(StringsList.Count)]));
for Idx := Pred(StringsList.Count-1) downto 0 do
begin
if TmpStrings[3] = TStrings(StringsList.Items[Idx])[3] then
StringsList.Delete(Idx)
else
TmpStrings.Assign(TStrings(StringsList.Items[Idx]));
end; //删除S5相同的行
StringsList.Sort(CompareS5); //按S5排序
TmpStrings.Assign(TStrings(StringsList.Items[Pred(StringsList.Count)]));
for Idx := Pred(StringsList.Count-1) downto 0 do
begin
if TmpStrings[4] = TStrings(StringsList.Items[Idx])[4] then
StringsList.Delete(Idx)
else
TmpStrings.Assign(TStrings(StringsList.Items[Idx]));
end; //把数据回写到MEMO
Memo.Clear;
for Idx := 0 to Pred(StringsList.Count) do
begin
Memo.Lines.Add(TStrings(StringsList.Items[Idx]).DelimitedText);
end; //把数据回写到文档
Memo.Lines.SaveToFile(OpenDialog.FileName);
end;
end;
========================
不会越界的。
======
20061217,D,B,DKDDA332021,ESA3332SS1
20061217,D,B,DKDDA332022,ESA3332SS2
20061217,D,B,DKDDA332023,ESA3332SS3
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,StrUtils;type
TForm1 = class(TForm)
Button1: TButton;
OpenDialog: TOpenDialog;
Memo: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
function CompareS4(Item1, Item2: Pointer): Integer;
begin
Result := CompareText(TStrings(Item1)[3], TStringList(Item2)[3]);
end;function CompareS5(Item1, Item2: Pointer): Integer;
begin
Result := CompareText(TStrings(Item1)[4], TStringList(Item2)[4]);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
StringsList: TList;
Strings: TStrings;
TmpStrings: TStrings;
Idx: Integer;
begin
if OpenDialog.Execute then
begin
Memo.Lines.LoadFromFile(OpenDialog.FileName); //把数据写入到List
StringsList := TList.Create;
for Idx := 0 to Pred(Memo.Lines.Count) do
begin
Strings := TStringList.Create;
Strings.Delimiter := ',';
Strings.DelimitedText := Memo.Lines[Idx];
StringsList.Add(Pointer(Strings));
end; TmpStrings := TStringList.Create; //删除S4相同的行
StringsList.Sort(CompareS4); //按S4排序
TmpStrings.Assign(TStrings(StringsList.Items[Pred(StringsList.Count)]));
for Idx := Pred(StringsList.Count-1) downto 0 do
begin
if TmpStrings[3] = TStrings(StringsList.Items[Idx])[3] then
StringsList.Delete(Idx)
else
TmpStrings.Assign(TStrings(StringsList.Items[Idx]));
end; //删除S5相同的行
StringsList.Sort(CompareS5); //按S5排序
TmpStrings.Assign(TStrings(StringsList.Items[Pred(StringsList.Count)]));
for Idx := Pred(StringsList.Count-1) downto 0 do
begin
if TmpStrings[4] = TStrings(StringsList.Items[Idx])[4] then
StringsList.Delete(Idx)
else
TmpStrings.Assign(TStrings(StringsList.Items[Idx]));
end; //把数据回写到MEMO
Memo.Clear;
for Idx := 0 to Pred(StringsList.Count) do
begin
Memo.Lines.Add(TStrings(StringsList.Items[Idx]).DelimitedText);
end; //把数据回写到文档
Memo.Lines.SaveToFile(OpenDialog.FileName);
end;
end;
end.
出错提示 http://www.kongqiqiu.com/down/text.jpg
运行到这里就出现了
//把数据写入到List
StringsList := TList.Create;
for Idx := 0 to Pred(Memo.Lines.Count) do
begin
if Pos(',', Memo.Lines[Idx]) > 0 then //去掉空行
begin
Strings := TStringList.Create;
Strings.Delimiter := ',';
Strings.DelimitedText := Memo.Lines[Idx];
StringsList.Add(Pointer(Strings));
end;
end;..............
StringsList := TList.Create;
for Idx := 0 to Pred(Memo.Lines.Count) do
begin
Strings := TStringList.Create;
Strings.Delimiter := ',';
Strings.DelimitedText := Memo.Lines[Idx];
if Strings.Count = 5 then //只处理含5列的行
StringsList.Add(Pointer(Strings));
end;