-5

I'm having trouble searching for strings in different memos and separating them. Let's go to the scene.

in Memo1 i have the following text

18049,25047,text4
18047,25046,text2
18048,25045,text3
18050,25048,text5
18046,25044,text1

and in Memo2

25049,9012646205,55315135004,adou4
25047,"",06252782912,textasidh
25046,"",44425660030,textblabla
25048,"",07649186806,textaldj

I need to separate the first digits up to the comma of memo2 and fetch into memo1 and add the complete lines. Memo1 + Memo2 in Memo3.

18046,25044,text1 25046,"",44425660030,textblabla
18047,25046,text2 25047,"",06252782912,textasidh
18048,25045,text3 25048,"",07649186806,textaldj
18049,25047,text4 25049,9012646205,55315135004,adou4

I've already tried using the function Split(Text, Delimitador: string): TSarray; but without success

var
   I, J: Byte;
   Z : String;
begin
  for I := 1 to 2 do
  begin
    for J := 0 to TMemo(FindComponent('Memo'+IntToStr(I))).Lines.Count -1 do
      begin
        Z := Memo2.Lines[J];
        if Pos(Split(Z, ',')[0],TMemo(FindComponent('Memo'+IntToStr(I))).Lines[J]) > 0 then
        Memo3.Lines.Add(TMemo(FindComponent('Memo'+IntToStr(I))).Lines[J]);
      end;
  end;
end;
Fake
  • 83
  • 2
  • 13
  • Have you looked at this question? https://stackoverflow.com/questions/2625707/split-a-string-into-an-array-of-strings-based-on-a-delimiter – John Easley Jun 30 '17 at 21:45
  • Also, you should always post some code that demonstrates where you've failed so that others can help you fix your code or your thinking. – John Easley Jun 30 '17 at 21:46
  • Why are you using a memo? Use a non GUI component. – David Heffernan Jun 30 '17 at 22:22
  • Hi @DavidHeffernan, No need to use gui interface, I'm just using it to "facilitate" a preview – Fake Jun 30 '17 at 22:24
  • 1
    You never assign anything to Z, so it is an empty string (nil), and after `Split` you take the second item (index `[1]`) from the resulting empty list. That is bound to go wrong. – Rudy Velthuis Jun 30 '17 at 22:44
  • @RudyVelthuis I changed the order by assigning a value to the variable and changing the index to 0 but it returns only the values of memo2 – Fake Jun 30 '17 at 22:55
  • Well, you do: `Z := Memo2.Lines[J];`, so it is obvious it only finds strings in that memo. – Rudy Velthuis Jun 30 '17 at 23:01

1 Answers1

-2

solved

var
I, J : byte;
begin
  for I := 0 to Memo1.Lines.Count -1 do
  begin
    for J := 0 to Memo2.Lines.Count -1 do
    begin
      if Pos(Split(Memo2.Lines[J], ',')[0],Memo1.Lines[I]) > 0 then
        Memo3.Lines.Add(Memo1.Lines[I]+' # '+Memo2.Lines[J]);
    end;
  end;
end;
Fake
  • 83
  • 2
  • 13
  • 2
    Rather use `Integer` than `Byte`: if `Lines.Count` is `0`, then `Count - 1` is negative. By using `Byte` as local variable, you don't save any memory anyway. Always prefer to use `Integer`for such situations. – Rudy Velthuis Jun 30 '17 at 23:07
  • 1
    FWIW, you don't test if the result of Split isn't empty. Don't try to produce complicated one-liners (as in your question: the J loop is terribly wrong). Make everything as clear as possible and use variables where necessary. – Rudy Velthuis Jun 30 '17 at 23:13
  • Indeed, `Proper Code` > `Short Code` – Jerry Dodge Jul 01 '17 at 03:15