Delphi - Find and Save Text Lines

Asked

Viewed 323 times

0

Good night,

I have a text file with several lines, where the lines that interest me are those that start with 'PU' and 'PD' where within these lines there are values that I would like to assign to two variables.

PW0.350,6;
PW0.350,7;
PW0.350,8;
LT;
SP1;
PU-3651 -2229;
PD-2851 -2229;
PD-2851 -1429;
PD-3651 -1429;
PD-3651 -2229;
SP0;

for example the first line that interests me 'PU-3651 -2229' would like to save (-3651 in variable x and -2229 in y) someone could help me

NOTE: I am already opening the file

if OpenDialog1.Execute then
  begin
      linhasarq := opendialog1.FileName;
      AssignFile(arquivo,opendialog1.FileName);
      Memo1.Text := opendialog1.FileName;
      reset(arquivo);
      while Not (Eof(arquivo)) do
      Begin
      ReadLn(arquivo,linhasarq);
      Memo2.Lines.Add(linhasarq);
      end;
      CloseFile(arquivo);
  • I’ve now used the command

2 answers

0


Good morning Guilhermeths.. Everything straight?? :)
Apparently it’s simple..
One of the ways that could be done would be so..:

Var
  Var_X,Var_Y,linhasarq : String;
begin
 if OpenDialog1.Execute then
  begin
      linhasarq := opendialog1.FileName;
      AssignFile(arquivo,opendialog1.FileName);
      Memo1.Text := opendialog1.FileName;
      reset(arquivo);
      while Not (Eof(arquivo)) do
      Begin
      ReadLn(arquivo,linhasarq);
      If (Pos('PU-',linhasarq) > 0) then
         begin
           Var_X:= Copy(linhasarq,3,Pos(' ',linhasarq)-1);//PU-3651 -2229;  Copia da posicao 3 = -, Ate a posicao espaco.. Retornando -3651
           Var_Y:= Copy(linhasarq,Pos(' ',linhasarq)+1,Length(linhasarq) - 1);
         end
      Else
        If (Pos('PD-',linhasarq) > 0) then
           begin
             Var_X:= Copy(linhasarq,3,Pos(' ',linhasarq)-1);//PD-2851 -2229;  Copia da posicao 3 = -, Ate a posicao espaco.. Retornando -2851
             Var_Y:= Copy(linhasarq,Pos(' ',linhasarq)+1,Length(linhasarq) - 1);
           end;

      RichEdit1.Lines.Add(linhasarq+ ' - Var_X '+Var_X+' Var_Y '+Var_Y);
      end;
      CloseFile(arquivo);
    end; 

end;

I put the values in the line below to be seen by Voce..
Richedit1.Lines.Add(linhasarq+ ' - Var_x '+Var_x+' Var_y '+Var_y);
I hope I understand your problem and have answered clearly..
Anything, just talk. Hugs :)

for:= 'PU-3651 -2229;';

Use of Pos(Substring,word): Integer;
Var_t:= Pos('U',word); // = 3

Use of COPY(word, from).

Var_x:= Copy(word,4,Pos(' ',word)-1);
He returns 3651 because we are asking for the Fourth character to the first space minus one ( -1)

  • Missing a "continue Else;" for cases where the line does not start with either PU or PD.

  • Ricardo, thank you very much for this explanation already helped me a lot. However I am receiving as value in the variable X '' -3651 - '' as I do to remove this ''-'' and in the variable Y this appearing the ; tried to use stringreplace, however I could not

  • Good morning Guilhermeths.. I complemented the example with the description of POS and COPY.. I think you can now understand better.. anything you can say :)

0

I don’t know which version of Delphi is using, so I considered it to be the latest (XE forward).

Uses
    System.RegularExpressions;

Var
  linhasarq, Valores: String;
  ValoresArray: TArray<string>;
  rgxSoNumero: TRegEx;

if OpenDialog1.Execute then
begin
    rgxSoNumero = TRegEx.Create('[a-zA-Z;]+');

    linhasarq := opendialog1.FileName;
    AssignFile(arquivo,opendialog1.FileName);
    Memo1.Text := opendialog1.FileName;
    reset(arquivo);
    while Not (Eof(arquivo)) do
    Begin
      ReadLn(arquivo,linhasarq);
      Memo2.Lines.Add(linhasarq);

      if (linhasarq.StartsWith('pu-'), true) or (linhasarq.StartsWith('pd-', true))
      begin
          Valores := rgxSoNumero.Replace(linhasarq, '').Trim; // Pega somente os números, sinal de menos e espaço da linha
          ValoresArray := Valores.Split([' ']);
          Memo2.Lines.Add('Valor X: '+ValoresArray[0]+' Valor Y: '+ValoresArray[1] );
      end;


    end;
    CloseFile(arquivo);
end;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.