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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

splitting text into seperate vars

Status
Not open for further replies.

lloydie2

Programmer
Apr 21, 2003
75
GB
I am writing a programme which needs to take text which is outputed form a machine using a serial port and the information then needs to be chopped up and sent into a database line by line. The line feed would probably be held in a buffer. How would I go about creating an array from the seperate elements of that string? ie:

A203 05/12/03 23:53:00 00:01:30 012345678 =

var[0] = A203
Var[1] = 05/12/03
var[2] = 23:53:00
var[3] = 00:01:30
var[4] = 012345678
 
I presume I should use the 'copy' function or is there an esaier way.
 
You don't really need an array. You can use a string list and access the elements with array notation. But if you want to move the elements to an array, you still can. Here is some code to get you started.
[blue]
Code:
procedure TfrmMain.Button1Click(Sender: TObject);
var
  oList:TStringList;
  i:integer;
  a:array[0..99] of string;
begin
  oList := TStringList.Create;
  oList.CommaText := 'A203 05/12/03 23:53:00 00:01:30 012345678 ';
  for i := 0 to oList.Count - 1 do
    a[i] := oList[i];
  oList.Free
  showmessage( a[0] + #13#10 +
               a[1] + #13#10 +
               a[2] + #13#10 +
               a[3] + #13#10 +
               a[4] + #13#10 );
end;
[/color]

 
Zathras. I am not getting it. I dont understand how Tstringlist is working. Does it look between the spaces until there is some text before it creates the element of the list? If so there may be a problem, as now and a again a character is inserted in between although it does not affect the overall length and position of the string.
 
It is a feature (bug?) of TStringList that it "breaks" on spaces between words (unless between double quotes).

If you could post some examples of the data you need to parse, perhaps I could help you more specifically. The solution I posted was based on the data you supplied.

 
I dont now how to attach a file on this forum so please find some example data. I have managed to get the desired result using the copy function, but yours looks more elegant, if it fits the bill.

A301 --> N02 03/12/03 18:11 00:00:05 ST 08006926002............... M 00:00 ................ PDQ.............
-----------------------------------------------------
A207 --> N02 03/12/03 18:18 00:04:33 ST I 07966323260............... M 00:00 ................ Anne Garbutt....
---------------------------------------------------
A209 --> N01 03/12/03 18:21 00:02:38 ST I 08456070200............... M 00:00 ................ Lucy Earle......

Remember to delete the Hyphens.
 
Ok, for that data here is one way to handle it:

Drop a TStringGrid and three TButtons on a form. Then use this code (don't forget to assign the event handlers to the events.):
[blue]
Code:
unit test;

interface

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

type
  TfrmMain = class(TForm)
    Button1: TButton;
    StringGrid1: TStringGrid;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    oList:TStringList;
  public
    { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.DFM}

{Automatically re-sizes string grid columns for best fit.}
procedure GridColAutoSize( Grid:TStringGrid );
var
  nCol, nRow, nWidth, nMaxWidth: integer;
begin
  with Grid as TStringGrid do begin
    for nCol := FixedCols to ColCount - 1 do begin
      nMaxWidth := 0;
      for nRow := 0 to RowCount - 1 do begin
        nWidth := Canvas.TextWidth( Cells[nCol,nRow] );
        if nWidth > nMaxWidth then nMaxWidth := nWidth;
      end;
      ColWidths[nCol] := nMaxWidth + 7;
    end;
  end;
end;

{Update a specified grid row from a string list -- with adjustments.}
procedure UpdateGrid( AGrid:TStringGrid; ARow:integer; AList:TStringList );
begin
  // If item[7] is not a single character, insert an extra column for alignment
  if Length(AList[7]) <> 1 then AList.Insert(7,'');
  // If AList has 14 items, concatenate 12 and 13 as one
  if AList.Count = 14 then
    begin
      AList[12] := AList[12] + ' ' + AList[13];
      AList.Delete(13);
    end;
  with AGrid do begin
    // If grid is not large enough, adjust row and column counts.
    if RowCount < (ARow+1) then RowCount := ARow+1;
    if ColCount < (AList.Count+1) then (ColCount := AList.Count);
    Rows[ARow].Assign(AList);
  end;
  GridColAutoSize( AGrid );
end;

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  oList := TStringList.Create;
end;

procedure TfrmMain.FormDestroy(Sender: TObject);
begin
  oList.Free;
end;

procedure TfrmMain.Button1Click(Sender: TObject);
begin
  oList.CommaText := 'A301  --> N02 03/12/03 18:11 00:00:05      ST        08006926002............... M 00:00            ................ PDQ.............';
  UpdateGrid( StringGrid1, 1,  oList );
end;

procedure TfrmMain.Button2Click(Sender: TObject);
begin
  oList.CommaText := '       A207  --> N02 03/12/03 18:18 00:04:33      ST I      07966323260............... M 00:00            ................ Anne Garbutt....';
  UpdateGrid( StringGrid1, 2,  oList );
end;

procedure TfrmMain.Button3Click(Sender: TObject);
begin
  oList.CommaText := '       A209  --> N01 03/12/03 18:21 00:02:38      ST I      08456070200............... M 00:00            ................ Lucy Earle......';
  UpdateGrid( StringGrid1, 3,  oList );
end;

end.
[/color]

You can add more code to handle other non-standard formats that may arise.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top