Question

I have data in string which is 6 / 8 or sometimes more lines like this :

part:    size   actsize  name
0dtm: 000a0000 00020000 "misc"
1dtm: 00480000 00020000 "every"
2dtm: 00300000 00020000 "hexs"
3dtm: 0fa00000 00020000 "stem"
4dtm: 02800000 00020000 "ache"
5dtm: 093a0000 00020000 "data"

i need from second to total/last line / first and fourth word from each line like this :

from total lines i need first and fourth word from each line

0dtm / misc // <-- needed data

same thing for total number of lines

2dtm / every // <-- needed data

note : number of lines is not always same

as the number of lines is not always same so i cannot use copy funstion any other suggestion ?

thx

Was it helpful?

Solution

The text format appears to be very rigid. Assuming that the data can be processed line by line, it looks like you can use pre-determined character indices to parse each line.

I'd start off by writing code to parse a single line into a record.

type
  TItem = record
    Part: string;
    Size: Integer;
    ActSize: Integer;
    Name: string;
  end;

function GetItemFromText(const Text: string): TItem;
begin
  Result.Part := Copy(Text, 1, 4);
  Result.Size := StrToInt('$'+Copy(Text, 7, 8));
  Result.ActSize := StrToInt('$'+Copy(Text, 16, 8));
  Result.Name := Copy(Text, 26, Length(Text)-26);
end;

Once we have this at hand it is a simple matter to process your data. First of all load it into a string list as a way to parse into separate lines.

var
  Items: TStringList;
....
Items := TStringList.Create;
Items.Text := MyData;

Then, process each line, remembering to skip the first line of headers:

var
  i: Integer;
....
for i := 1 to Items.Count-1 do 
begin
  Item := GetItemFromText(Items[i]);
  // do whatever needs to be done with the content of Item
end;

OTHER TIPS

Let's your strings are in TStringList or string array. You can use TStrings.CommaText or DelimitedText properties to extract parts of string:

TempList := TStringList.Create; // helper list
for i := 0 to List.Count - 1 do begin //or to High(array)
  TempList.CommaText := List[i];
  if TempList.Count >= 4 then begin //use separated elements
    FirstData := TempList[0];
    FourthData := TempList[3];
  end;
end;
TempList.Free;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top