-4

I have these data:

CMD210 STA_ 99.0 uS Temp 22.1 C

CMD210 STAB 99.9 uS Temp 22 C

CMD210 STAB 0.1 mS Temp 22.1 C

CMD210 STA_ 99.5 uS Temp 22.1 C

CMD210 STAB 99.4 uS Temp 22 C

CMD210 ST__ 99.0 uS Temp 22.2 C

CMD210 STAB 0.1 mS Temp 22 C

CMD210 STAB 99.3 uS Temp 22.2 C

I would like to have a program that display the temperature from memo for exampel in a listbox.

I know I have to get loop and something with 2 char with 'p' and 'c', because the number is between those to letters....

procedure TForm1.Button4Click(Sender: TObject);
  var
    midlet,midler:char;
    resultat,x:integer;
    linecount,index:integer;
    found: boolean;
begin
midlet:= 'p';
  midler:='C';
  index:=0;
  resultat:=midlet+x+midler
  found := false;
  linecount := Memo1.lines.count;
 while index<= linecount - 1 do
 begin
   if x = memo1.lines[index] then
   found := true;
   index :=index + 1;
   end
 if found = true then
   ListBox1.text:= floattostrF(x,ffFixed,15,2);
 end; 

1 Answers1

0

There are several problems in your example so this answer will be limited to "how extracting and converting the temperature from a line". You have fundamentally two ways to achieve the task:

  • use the regular expressions.
  • write a custom parser.

the custom parser is quite easy to write:

  • accumulate non-blank chars in an identifier.
  • if the identifier is equal to Temp then define a flag.
  • convert the identifier to a double if the flag is defined and if someting's been accumulated.

example:

program Project1;

uses
  sysutils;

const
  line1 = 'CMD210 STAB 99.3 uS Temp 22.2 C';
  line2 = 'CMD210 STAB 0.1 mS Temp 22 C';
  line3 = 'it is quite hot over there Temp 55.123456 C';
  line4 = 'bla bla bla bla 12.564 C';
  line5 = '';

function getTemperature(aLine: string): double;
var
  reader: PChar;
  identifier: string;
  AccumulateTemp: boolean;
const
  _Nan = 0/0;
begin
  // initialize local variables.
  identifier := '';
  AccumulateTemp := false;
  // success can be tested with isNan()
  result := _Nan;
  // add a distinct terminal char:
  aLine := aLine + #0;
  reader := @aLine[1];

  while(true) do begin
    if reader^= #0 then
      exit;
    // blank: test the identifier
    if reader^ in [#9, ' '] then
    begin
      if AccumulateTemp then
      begin
        if not TryStrToFloat(identifier, result) then
          result := _Nan;
        AccumulateTemp := false;
        exit;
      end;
      if identifier = 'Temp' then
        AccumulateTemp := true;
      identifier := '';
    end else
    // accumulate
      identifier := identifier + reader^;
    Inc(reader);
  end;
end;

begin
  DecimalSeparator := '.';
  writeln( format('%.7f', [getTemperature(line1)])  );
  writeln( format('%.7f', [getTemperature(line2)])  );
  writeln( format('%.7f', [getTemperature(line3)])  );
  writeln( format('%.7f', [getTemperature(line4)])  );
  writeln( format('%.7f', [getTemperature(line5)])  );
  readln;
end.

which outputs

22.2000000
22.0000000
55.1234560
Nan
Nan
Abstract type
  • 1,901
  • 2
  • 15
  • 26