Results 1 to 6 of 6

Thread: Text Processing

  1. #1
    Join Date
    Sep 2004
    Posts
    281
    Rep Power
    0

    Default Text Processing

    Hi all...i used a program named sclist to generate a list of services on my systm. A snapshot is below
    C:\> sclist > mylist_of_servies
    --------------------------------------------
    - Service list for Local Machine
    --------------------------------------------
    stopped Alerter Alerter
    stopped Apache Apache
    stopped Apache2 Apache2

    i want to access each column individually.
    i tried using readln(x,y,z) with no success. ( x,y,z are variables of type string ) .

    thaat function/procedure which ever gives me one row with all three items
    stopped Alerter Alerter

    Any ideas :-- i think it has somthing to do withe the way the program outputs the data.

  2. #2
    Join Date
    May 2003
    Posts
    229
    Rep Power
    0

    Default Re: Text Processing

    You did remember to skip over the first three lines of the output, correct? You say that readln(x,y,z) doesn't work, but you didn't say what happens when you tried it. I am assuming that you ran the sclist program and redirected the output into a text file (like the snapshot you gave below) and then you want to be able to read the file in your program. How are you running the program which is supposed to read the data.

    Quote Originally Posted by Artificial_Intelligence
    Hi all...i used a program named sclist to generate a list of services on my systm. A snapshot is below
    C:\> sclist > mylist_of_servies
    --------------------------------------------
    - Service list for Local Machine
    --------------------------------------------
    stopped Alerter Alerter
    stopped Apache Apache
    stopped Apache2 Apache2

    i want to access each column individually.
    i tried using readln(x,y,z) with no success. ( x,y,z are variables of type string ) .

    thaat function/procedure which ever gives me one row with all three items
    stopped Alerter Alerter

    Any ideas :-- i think it has somthing to do withe the way the program outputs the data.

  3. #3
    Join Date
    Sep 2004
    Posts
    281
    Rep Power
    0

    Default Re: Text Processing

    When i run the readln(x,y,z) ; (x,y,z are variables ) the first variable (X) was filled each time :below: while the other variables where left empty and yes i am aware the first couple of lines are not usefull



    X:= stopped Alerter Alerter
    stopped Apache Apache
    stopped Apache2 Apache2

    Y:=' ';
    Z:=' ';

  4. #4
    Join Date
    Sep 2004
    Posts
    281
    Rep Power
    0

    Default Re: Text Processing

    Hi all delphi users -----: Got a solution from another forum . It is listed
    below.

    Code Starts here.
    /************************************************** *******/
    procedure TForm1.Button1Click(Sender: TObject);
    var
    s: string;
    i,a: integer;
    strings: array [0..2] of String;
    begin
    s := 'stopped AppMgmt Application Management';
    a := 0;
    for i := 1 to Length(s) do begin
    if (s[i] = ' ') and (a < 2) then begin
    inc(a);
    Continue;
    end;
    strings[a] := strings[a] + s[i];
    end;
    ListBox1.Items.Add(strings[0]);
    ListBox2.Items.Add(strings[1]);
    ListBox3.Items.Add(strings[2]);
    end;

    /**********************End_Of_Code***************** *****/

  5. #5
    Join Date
    Jan 2006
    Posts
    74
    Rep Power
    0

    Default

    what was the name of the forum?

  6. #6
    Join Date
    Mar 2003
    Posts
    492
    Rep Power
    0

    Default

    Here is a quick parser if you wish to parse multiple lines from a text file with that structure.

    Code:
    unit quickparse;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls, StrUtils;
    
    const DumpList = 'stopped Alerter Alerter'#13 +
                     'stopped Apache Apache'#13 +
                     'stopped Apache2 Apache2';
    
    type
      PServiceDumpData = ^TServiceDumpData;
      TServiceDumpData = record
        Status : String[255];
        Process1 : String[255];
        Process2 : String[255];
      end;
    
      TForm1 = class(TForm)
        Button1: TButton;
        ListBox1: TListBox;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure ListBox1Click(Sender: TObject);
      private
    
      public
    
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      DumpStrings : TStringList;
      DumpString : String;
      Delimiter : Char;
      LastPos : Integer;
      i : Integer;
      c : Integer;
      ColCount : Integer;
      ServiceDumpData : PServiceDumpData;
    begin
      Delimiter := ' ';
      DumpStrings := TStringList.Create;
      DumpStrings.Clear;
      DumpStrings.Text := DumpList;
    
      ListBox1.Items.BeginUpdate;
      ListBox1.Items.Clear;
      for i := 0 to DumpStrings.Count - 1 do
      begin
        ColCount := 0;     // reset the column count
        LastPos := 0;      // default last position
        New(ServiceDumpData);   // create a new record to hold the data
        DumpString := DumpStrings[i];   // assign the dumpstring
    
        for c := 1 to Length(DumpString) do    // iterate through the string till we find a delimiter
        begin
          if (DumpString[c] = Delimiter) or (c= Length(DumpString)) then    // is this a delimiter or Eos?
          begin
            Inc(ColCount);                     // let's now parse for columns
            case ColCount of
              1 : ServiceDumpData.Status := Copy(DumpString, LastPos + 1, c - LastPos);   // since it's the first delimiter it's column 1
              2 : ServiceDumpData.Process1 := Copy(DumpString, LastPos + 1, c - LastPos); // and so on and so on
              3 : ServiceDumpData.Process2 := Copy(DumpString, LastPos + 1, c - LastPos); // ......
            end;                                                          // we can remove the old column from the string;
            LastPos := c;       // set the last pos as a reference
          end;
        end;
        ListBox1.Items.AddObject(ServiceDumpData.Status + #9 +
                                 ServiceDumpData.Process1 + #9 +
                                 ServiceDumpData.Process2, TObject(ServiceDumpData));
      end;
      ListBox1.Items.EndUpdate;
      DumpStrings.Free;
    end;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      ListBox1.TabWidth := 64; // gives the listbox a multi-column effect when used with tabs (^I or #9 ASCII character)
    end;
    
    procedure TForm1.ListBox1Click(Sender: TObject);
    begin
      if TListBox(Sender).ItemIndex = -1 then
        Exit;
      ShowMessage(PServiceDumpData(TListBox(Sender).Items.Objects[TListBox(Sender).ItemIndex])^.Status);
    end;
    
    end.

    The use of records is very efficient. You can use classes, but the point is that since you said you dumped it to a text file, you can use the LoadFromFile method of a TString descentend to get the data from the file, given that the dump follows that 3 column format and get it parsed. Because you will be using records or classes you can attach it to the ListBox items and recall it in the future when needed; as shown in the ListBox.OnClick event handler.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •