To Loop or not to Loop lazarus / 62-General ---------------------------------------------------------------------- petevick · 2022-02-22 15:15 This is just a 'good coding' query really. I have the following snippet of code in a project, FileStrList is a TStringList. I keep on thinking it needs to go in a loop of some kind, but I don't see any easy and logical way of increasing x for each line, maybe via If Then, but then I can't help feeling that the effort really outstrips any benefit. So, is there any easy way ?? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i        FileStrList[x] := FileStrList[x] + '/*';       FileStrList[x+20] := FileStrList[x+20] + '/*';  // + 20       FileStrList[x+26] := FileStrList[x+26] + '/*';  // + 6       FileStrList[x+32] := FileStrList[x+32] + '/*';  // + 6       FileStrList[x+64] := FileStrList[x+64] + '/*';  // + 32       FileStrList[x+97] := FileStrList[x+97] + '/*';  // + 33       FileStrList[x+130] := FileStrList[x+130] + '/*';  // + 33 ---------------------------------------------------------------------- paweld · 2022-02-22 15:45 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  arr: Array [0..6] of Integer = (0, 20, 6, 6, 32, 33, 33);  i, x: Integer;begin  x := 10;  //start index  for i := Low(arr) to High(arr) do  begin    x := x + arr[i];    if x < FileStrList.Count then      FileStrList[x] := FileStrList[x] + '/*';  end;end; ---------------------------------------------------------------------- petevick · 2022-02-22 16:06 Well I'll be damned. That's pretty neat paweld, never even thought about an array ---------------------------------------------------------------------- wp · 2022-02-22 16:25 OK, you asked for an alternate solution, but just my 2 cents: In my eyes the initial code is much clearer than the array variant, I understand it immediately, but I need to think in the array variant what's going on here. From this point of view, trivial code is often "better" than elegant code. ---------------------------------------------------------------------- winni · 2022-02-22 16:26 Hi! But the code of paweld will not work because the  value of x is always inremented and does not stay at 10. Do it this way: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     var    arr: Array [0..6] of Integer = (0, 20, 26,32, 64, 97, 130);      i, x,idx: Integer;    begin      x := 10;  //start index      for i in arr do      begin        idx:= x + arr[i];        if idx < FileStrList.Count then          FileStrList[idx] := FileStrList[idx] + '/*';      end;    end; Winni ---------------------------------------------------------------------- engkin · 2022-02-22 16:35 I think using hard coded values is not a good practice. The text in FileStrList seems like a template. Is it? Specifically beginning at line x? ---------------------------------------------------------------------- petevick · 2022-02-22 16:50 Quote from: engkin on February 22, 2022, 05:35:33 pmI think using hard coded values is not a good practice. The text in FileStrList seems like a template. Is it? Specifically beginning at line x? yes, kind of a template, it's a QT stylesheet, but x can vary ---------------------------------------------------------------------- petevick · 2022-02-22 16:54 Quote from: wp on February 22, 2022, 05:25:54 pmOK, you asked for an alternate solution, but just my 2 cents: In my eyes the initial code is much clearer than the array variant, I understand it immediately, but I need to think in the array variant what's going on here. From this point of view, trivial code is often "better" than elegant code. I totally agree with you, I was just interested how others would, or could handle it. If there were more lines then the array method would be more favourable I think.. Most likely it'll stay as is. ---------------------------------------------------------------------- engkin · 2022-02-22 17:50 Quote from: petevick on February 22, 2022, 05:50:00 pmQuote from: engkin on February 22, 2022, 05:35:33 pmI think using hard coded values is not a good practice. The text in FileStrList seems like a template. Is it? Specifically beginning at line x? yes, kind of a template, it's a QT stylesheet, but x can vary Sounds like you could benefit from using Format instead of adding comments to specific lines individually. ---------------------------------------------------------------------- petevick · 2022-02-22 18:01 Quote from: engkin on February 22, 2022, 06:50:51 pmSounds like you could benefit from using Format instead of adding comments to specific lines individually. Blimey, I wasn't even aware of the Format function, I'll take a look at that. Thank you engkin  Every day's a learning day, even at my age. ---------------------------------------------------------------------- Kays · 2022-02-22 18:20 Quote from: winni on February 22, 2022, 05:26:07 pmBut the code of paweld will not work because the  value of x is always inremented and does not stay at 10.Yes, it will: His array is using differences while you are using the absolute offset. ---------------------------------------------------------------------- bytebites · 2022-02-22 18:45 The starting index is wrong, it should be zero. ---------------------------------------------------------------------- glorfin · 2022-02-23 16:52 There is for ... in construction, which is more suitable for the solution with array, because you do not need an index variable.