2010-03-05 53 views
1

我有一個字符串列表(TStrings),其中有幾千項。我需要以100個爲一組來處理它們。我基本上想知道在Delphi中執行循環的最佳方法是什麼。當我試圖弄清楚時,我正在打磚牆。德爾福 - 正確的方式來頁面,雖然數據

感謝


unit Unit2; 

interface 

uses 
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
    Dialogs, StdCtrls; 

type 
    TForm2 = class(TForm) 
    Memo1: TMemo; 
    Memo2: TMemo; 
    Button1: TButton; 
    procedure Button1Click(Sender: TObject); 
    private 
    { Private declarations } 
    public 
    { Public declarations } 
    end; 

var 
    Form2: TForm2; 

implementation 

Uses math; 
{$R *.dfm} 

procedure TForm2.Button1Click(Sender: TObject); 
var 
    I:Integer; 
    pages:Integer; 
    str:string; 
begin 
    pages:= ceil(memo1.Lines.Count/100) ; 
    memo2.Lines.add('Total Pages: '+inttostr(pages)); 
    memo2.Lines.add('Total Items: '+inttostr(memo1.Lines.Count)); 


    // Should just do in batches of 100 VS entire list 
    for I := 0 to memo1.lines.Count - 1 do 
    begin 
    if str > '' then 
    str:= str+#10+ memo1.Lines.Strings[i] 
    else 
    str:= memo1.Lines.Strings[i]; 
    end; 
    //I need to stop here every 100 items, then process the items. 
    memo2.Lines.Add(str); 

end; 

end. 

實例形式


object Form2: TForm2 
    Left = 0 
    Top = 0 
    Caption = 'Form2' 
    ClientHeight = 245 
    ClientWidth = 527 
    Color = clBtnFace 
    Font.Charset = DEFAULT_CHARSET 
    Font.Color = clWindowText 
    Font.Height = -11 
    Font.Name = 'Tahoma' 
    Font.Style = [] 
    OldCreateOrder = False 
    PixelsPerInch = 96 
    TextHeight = 13 
    object Memo1: TMemo 
    Left = 16 
    Top = 8 
    Width = 209 
    Height = 175 
    Lines.Strings = (
     '4xlt columbia thunder storm jacket' 
     '5 things about thunder storms' 
     'a thunder storm with a lot of thunder ' 
     'and lighting sccreensaver' 
     'a thunder storm with a lot of thunder ' 
     'and lighting screensaver with no nag ' 
     'screens' 
     'all about thunder storms' 
     'all about thunderstorms for kids' 
     'amazing tornado videos and ' 
     'thunderstorm videos' 
     'are thunder storms louder in ohio?' 
     'bad thunder storms' 
     'bathing in thunder storm' 
     'best thunderstorm pictures' 
     'cartoon thunder storms' 
     'celtic thunder storm' 
     'central valley thunder storm' 
     'chicago thunderstorm pictures' 
     'cool thunderstorm pictures' 
     'current thunderstorm warnings' 
     'does thunder storms in december mean ' 
     'snow will be coming' 
     'facts about thunderstorms for kids' 
     'facts on thunderstorms for kids' 
     'fedex thunderstorm video' 
     'florida thunderstorms facts' 
     'free relaxing thunderstorm music' 
     'free soothing thunderstorm sounds ' 
     'online' 
     'free thunderstorm mp3' 
     'free thunderstorm mp3 download' 
     'free thunderstorm mp3 downloads' 
     'free thunderstorm mp3s' 
     'free thunderstorm music' 
     'free thunderstorm pictures' 
     'free thunderstorm sound effects' 
     'free thunderstorm sounds' 
     'free thunderstorm sounds cd' 
     'free thunderstorm sounds mp3' 
     'free thunderstorm sounds online' 
     'free thunderstorm soundscape' 
     'free thunderstorm video' 
     'free thunderstorm video download' 
     'free thunderstorm videos' 
     'god of storm and thunder' 
     'horses storm thunder rain' 
     'how do thunder storms form' 
     'how far away is a thunder storm' 
     'how long do thunder storms last' 
     'ice cube in a thunder storm' 
     'indoor thunderstorm safety tips' 
     'information about thunderstorms for kids' 
     'interesting thunderstorm facts' 
     'is it dangerous to shower during thunder ' 
     'storm' 
     'is there frequently thunder during snow ' 
     'storms' 
     'isolated thunderstorms' 
     'it'#39's just a thunder storm baby there is ' 
     'nothing you should fear lyrics' 
     'lightning & thunder storm safety' 
     'lightning and thunderstorm facts' 
     'lightning and thunderstorms facts' 
     'lightning and thunderstorms for kids' 
     'listen to thunderstorm sounds online' 
     'mississauga thunder storm' 
     'nature sounds free mp3 thunder storm' 
     'only about thunderstorms facts' 
     'original storm deep thunderstick' 
     'phone use during thunder storms' 
     'pictures of thunderstorms' 
     'pocono thunder storm' 
     'posters of thunder storms' 
     'power rangers ninja storm' 
     'power rangers thunder storm' 
     'power rangers thunder storm cast' 
     'power rangers thunder storm games' 
     'power rangers thunder storm morphers' 
     'power rangers thunder storm part 1' 
     'power rangers thunder storm part 2' 
     'power rangers thunderstorm' 
     'power rangers thunderstorm cannon' 
     'power rangers thunderstorm deluxe ' 
     'megazord' 
     'power rangers thunderstorm games' 
     'power rangers thunderstorm megazord' 
     'power rangers thunderstorm part 2' 
     'power rangers thunderstorm pictures' 
     'power rnager ninja storm thunder staff' 
     'powerful thunder and lightning storms' 
     'precambrian thunder storms' 
     'rain thunderstorm mp3' 
     'rain thunderstorm pictures' 
     'relaxing thunderstorm music' 
     'reminds me of ohio river thunder lighten ' 
     'storms' 
     'sacramento thunder storm' 
     'safety tips for when your caught in a ' 
     'thunder storm' 
     'scattered thunderstorms' 
     'schemer puts his head in the thunder ' 
     'storm' 
     'sedative thunder storm' 
     'server thunder storms' 
     'severe supercell thunderstorm pictures' 
     'severe thunder storm pictures' 
     'severe thunder storms' 
     'severe thunderstorm facts' 
     'severe thunderstorm pictures' 
     'severe thunderstorm pictures hail' 
     'severe thunderstorm pictures in alberta' 
     'severe thunderstorm pictures tornado' 
     'severe thunderstorm safety' 
     'severe thunderstorm safety tips' 
     'severe thunderstorm videos' 
     'severe thunderstorm warning' 
     'severe thunderstorm warning los ' 
     'angeles' 
     'severe thunderstorm warning signs' 
     'severe thunderstorm warnings' 
     'severe thunderstorms' 
     'severe thunderstorms facts' 
     'shakespeare use thunder storm for ' 
     'cosmic disorder julius caesar' 
     'soothing thunderstorm sounds online' 
     'sound effects of severe thunder storm' 
     'sound of rain storm finger snapping ' 
     'thunder chorus' 
     'split thunder storm' 
     'storm 3d thunder power' 
     'storm dark thunder' 
     'storm dark thunder bowling ball' 
     'storm dark thunder bowling ball sale' 
     'storm dark thunder for sale' 
     'storm dark thunder pearl' 
     'storm dark thunder pearl bowling ball' 
     'storm dark thunder review' 
     'storm dark thunder shirt' 
     'storm dark thunderball' 
     'storm deep thunder' 
     'storm deep thunder 11' 
     'storm deep thunder 15' 
     'storm deep thunder 15 lure' 
     'storm deep thunder 2' 
     'storm deep thunder lures' 
     'storm deep thunderstick' 
     'storm deep thunderstick crankbaits' 
     'storm deep thunderstick dts09' 
     'storm deep thunderstick jr' 
     'storm deep thunderstick lures' 
     'storm deep thundersticks' 
     'storm rolling thunder 3 ball roller' 
     'storm rolling thunder bowling bag' 
     'storm rolling thunder three ball bowling ' 
     'bag' 
     'storm shallow thunder' 
     'storm shallow thunder 15' 
     'storm thunder claw' 
     'storm thunder craw' 
     'storm watches thunder' 
     'storms with constant lightning and ' 
     'thunder non-stop' 
     'supercell thunder storms' 
     'supercell thunderstorm pictures' 
     'supercell thunderstorms' 
     'swimming pools thunder storms' 
     'tampa + lightning strikes + thunder ' 
     'storms' 
     'texas thunderstorm pictures' 
     'texas thunderstorm warnings' 
     'thunder and lightning storm' 
     'thunder and lighting storms' 
     'thunder and lightning storms' 
     'thunder bay snow storm video' 
     'thunder storm' 
     'thunder storm and windmill' 
     'thunder storm cd' 
     'thunder storm cloud' 
     'thunder storm clouds' 
     'thunder storm dog peppermint oil' 
     'thunder storm in winter' 
     'thunder storm in winter and weather ' 
     'prediction' 
     'thunder storm lx-3 & road blaster psx ' 
     'download' 
     'thunder storm occurances' 
     'thunder storm photos' 
     'thunder storm poems' 
     'thunder storm safety' 
     'thunder storm sign' 
     'thunder storm sounds' 
     'thunder storms' 
     'thunder storms and deaths' 
     'thunder storms and ilghting' 
     'thunder storms and lighting' 
     'thunder storms cd' 
     'thunder storms in the arctic arctic ' 
     'weather' 
     'thunder storms in winter' 
     'thunder storms on you tub' 
     'thunder storms pics' 
     'thunder storms with rain' 
     'thunderstorm' 
     'thunderstorm backgrounds' 
     'thunderstorm capital' 
     'thunderstorm capital 2008 dorfman' 
     'thunderstorm capital in boston' 
     'thunderstorm capital llc' 
     'thunderstorm capital of canada' 
     'thunderstorm capital of the us' 
     'thunderstorm capital of the world' 
     'thunderstorm facts' 
     'thunderstorm facts for kids' 
     'thunderstorm facts hail' 
     'thunderstorm facts tornadoes' 
     'thunderstorm mp3' 
     'thunderstorm mp3 download' 
     'thunderstorm mp3 download free' 
     'thunderstorm mp3 downloads' 
     'thunderstorm mp3 downloads free' 
     'thunderstorm mp3 files' 
     'thunderstorm mp3 free' 
     'thunderstorm mp3 free download' 
     'thunderstorm mp3 free downloads' 
     'thunderstorm mp3 torrent' 
     'thunderstorm mp3s' 
     'thunderstorm music' 
     'thunderstorm music cd' 
     'thunderstorm music downloads' 
     'thunderstorm music free' 
     'thunderstorm music playlists' 
     'thunderstorm music rain' 
     'thunderstorm pics' 
     'thunderstorm pictures' 
     'thunderstorm pictures for kids' 
     'thunderstorm safety' 
     'thunderstorm safety for kids' 
     'thunderstorm safety precautions' 
     'thunderstorm safety procedures' 
     'thunderstorm safety rules' 
     'thunderstorm safety tips' 
     'thunderstorm safety tips for kids' 
     'thunderstorm safety tips shelter' 
     'thunderstorm safety tips trees' 
     'thunderstorm sound effects' 
     'thunderstorm sound effects cd' 
     'thunderstorm sound effects download' 
     'thunderstorm sound effects free' 
     'thunderstorm sound effects free ' 
     'download' 
     'thunderstorm sound effects free music ' 
     'feature audio' 
     'thunderstorm sound effects mp3' 
     'thunderstorm sound effects rain' 
     'thunderstorm sounds' 
     'thunderstorm sounds cd' 
     'thunderstorm sounds download' 
     'thunderstorm sounds for sleep' 
     'thunderstorm sounds for sleeping' 
     'thunderstorm sounds free' 
     'thunderstorm sounds free download' 
     'thunderstorm sounds free downloads' 
     'thunderstorm sounds mp3' 
     'thunderstorm sounds mp3 download' 
     'thunderstorm sounds mp3 free' 
     'thunderstorm sounds online' 
     'thunderstorm sounds online for free' 
     'thunderstorm sounds online free' 
     'thunderstorm sounds sleep' 
     'thunderstorm sounds streaming' 
     'thunderstorm sounds torrent' 
     'thunderstorm soundscape' 
     'thunderstorm soundscapes' 
     'thunderstorm video' 
     'thunderstorm video clips' 
     'thunderstorm video download' 
     'thunderstorm video downloads' 
     'thunderstorm videos' 
     'thunderstorm videos for kids' 
     'thunderstorm videos lightning' 
     'thunderstorm videos online' 
     'thunderstorm wallpaper' 
     'thunderstorm warning' 
     'thunderstorm warning brisbane' 
     'thunderstorm warning definition' 
     'thunderstorm warning los angeles' 
     'thunderstorm warning san diego' 
     'thunderstorm warning san mateo county' 
     'thunderstorm warning santa barbara' 
     'thunderstorm warning santa clara' 
     'thunderstorm warning santa clara ' 
     'county' 
     'thunderstorm warning signal' 
     'thunderstorm warning signs' 
     'thunderstorm warning vs watch' 
     'thunderstorm warnings' 
     'thunderstorm warnings and watches' 
     'thunderstorm warnings for nj' 
     'thunderstorm warnings qld' 
     'thunderstorms' 
     'thunderstorms facts' 
     'thunderstorms facts for kids' 
     'thunderstorms for kids' 
     'tornados and thunder storms animated' 
     'understanding thunderstorms for kids' 
     'watch thunderstorm videos' 
     'weather underground forecast ' 
     'thunderstorms' 
     'what causes thunder storms' 
     'what is a thunder storm' 
     'where d thunder storms occur') 
    TabOrder = 0 
    end 
    object Memo2: TMemo 
    Left = 240 
    Top = 8 
    Width = 265 
    Height = 129 
    Lines.Strings = (
     'Memo2') 
    TabOrder = 1 
    end 
    object Button1: TButton 
    Left = 384 
    Top = 184 
    Width = 75 
    Height = 25 
    Caption = 'Button1' 
    TabOrder = 2 
    OnClick = Button1Click 
    end 
end 
+0

你是否在項目中使用了任何.NET對象?也許更精確一點的精度會幫助我們更好地理解你的需求。謝謝! – 2010-03-05 00:16:54

+0

不,我使用普通的delphi。 我基本上想知道什麼樣的循環是最好的。使用。我應該如何構建它。 – Brad 2010-03-05 00:36:27

+0

爲什麼你需要一次處理100個?你無法一次完成所有項目,但是你根本不關心實際的數字是什麼樣的過程?您對時間和用戶界面的阻塞感到擔憂嗎?還是關於每個處理塊的數據集大小?或者是什麼? – 2010-03-17 17:08:26

回答

4

使用輔助的TStringList:

procedure TForm2.Button1Click(Sender: TObject); 
var 
    I:Integer; 
    pages:Integer; 
    items: TStringList; 
begin 
    pages := ceil(Memo1.Lines.Count/100); 
    Memo2.Lines.add('Total Pages: ' + IntToStr(pages)); 
    Memo2.Lines.add('Total Items: '+ IntToStr(Memo1.Lines.Count)); 

    items := TStringList.Create; 
    try 
    for I := 0 to Memo1.lines.Count - 1 do 
    begin 
     items.Add(Memo1.Lines.Strings[i]); 
     if (items.Count mod 100) = 0 then 
     begin 
     // process the current list of items... 
     Memo2.Lines.AddStrings(items); 
     items.Clear; 
     end; 
    end; 
    if items.Count > 0 then 
    begin 
     // process the remaining items... 
     Memo2.Lines.AddStrings(items); 
    end; 
    finally 
    items.Free; 
    end; 
end; 
+0

謝謝雷米,完美的作品,我曾試過類似的東西,但我有一個高級時刻..哈哈 – Brad 2010-03-05 07:28:11

1

次要的StringList是好的...但如果性能是一個問題可言,只是使用嵌套循環

procedure TForm2.Button1Click(Sender: TObject); 
var 
    I:Integer; 
    pages:Integer; 
    str:string; 
begin 
    pages:= ceil(memo1.Lines.Count/100) ; 
    memo2.Lines.add('Total Pages: '+inttostr(pages)); 
    memo2.Lines.add('Total Items: '+inttostr(memo1.Lines.Count)); 


    // Should just do in batches of 100 VS entire list 
    for I := 0 to pages - 1 do 
    begin 
    For J := I * 100 to Min(I*100 + 99,memo1.lines.Count) do 
    begin 
     if str > '' then 
     str:= str+#10+ memo1.Lines.Strings[J] 
     else 
     str:= memo1.Lines.Strings[J]; 
    end; 
    //Process Here 
    end; 
    //I need to stop here every 100 items, then process the items. 
    memo2.Lines.Add(str); 

end;