show the content of an array lazarus / 21-General ---------------------------------------------------------------------- aja_ttara · 2025-06-13 22:39 Is there a way to display the whole content of an array variable? For example, I'd like the code to show me the content of the variable 'group': var group:array[1..10] of integer = (10,20,30,40,50,60,71,80,90,91); begin   write(group);   read() end. This doesn't work as the compiler tells me "Can't read or write variables of this type". I know I can use a 'for' cycle for i:= 1 to Length(group) do   write(group, ' ');   read() end. but is there any simpler, quicker way? ---------------------------------------------------------------------- speter · 2025-06-14 00:09 The cleanest way to iterate through the array - which I believe you need to do - is something like: 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  i : integer;  group: array of integer = (10,20,30,40,50,60,71,80,90,91);begin  for i in group do    write(i,' ');  readln();end.  A alternative would be to create a procedure that takes the array as a parameter, then writes the array content... cheers S. ---------------------------------------------------------------------- Thaddy · 2025-06-14 12:10 Your example is OK, but there may be some formatting requirements for arrays, so I suggest to add a parameter where he can do a modulo break.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 {$ifdef fpc}{$mode objfpc}{$endif}uses types;procedure arrayprint(const values:TIntegerDynArray;BreakAt:Integer);var  i : integer; begin  for i := 0 to High(values) do  begin    write(values[i],' ');     if (succ(i) mod BreakAt = 0) and         ( i > 1) and         (i < high(values)) then           writeln;  end;  end; var  group: array of integer = (10,20,30,40,50,60,71,80,90,91);begin  arrayprint(group,3);end.This prevents large arrays being printed on a single line. ---------------------------------------------------------------------- aja_ttara · 2025-06-14 12:12 Quote from: speter on June 14, 2025, 02:09:19 amThe cleanest way to iterate through the array - which I believe you need to do - is something like: 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  i : integer;  group: array of integer = (10,20,30,40,50,60,71,80,90,91);begin  for i in group do    write(i,' ');  readln();end.  A alternative would be to create a procedure that takes the array as a parameter, then writes the array content... cheers S. +1 I didn't think about writing an ad hoc procedure, thank you! ---------------------------------------------------------------------- Thaddy · 2025-06-14 12:23 Posts crossed but better look at my answer. The use of in is not appropriate here, because you can not break at the index. ---------------------------------------------------------------------- aja_ttara · 2025-06-14 12:33 Quote from: Thaddy on June 14, 2025, 02:23:38 pmPosts crossed but better look at my answer. The use of in is not appropiate here. Thank you but I'm afraid your answer is too advanced for me, I don't understand it ---------------------------------------------------------------------- Thaddy · 2025-06-14 12:38 I will explain: If an array is really really big and you use the in solution, which is not bad as such, you can not break a display line after a given number of writes for a single line. It happens to be that you can use mod to determine if you want to add a linebreak with writeln. Except for the modulo for 1 being 0, so I look ahead (succ()). Even if the array is really large, we can display it neatly in blocks of the number of array elements you want per line. I hope that helps. Will adapt my example in a minute or so. [EDIT] 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 {$ifdef fpc}{$mode objfpc}{$endif}procedure arrayprint(const values:array of integer;BreakAt:Integer; width:integer);var  i : integer; begin  for i := 0 to High(values) do  begin    write(values[i]:width,' ');     if (succ(i) mod BreakAt = 0) and         ( i > 1) and         (i < high(values)) then           writeln;  end;  end;var  group: array of integer;  i:integer;begin  setlength(group,100); //0 to 99  // initialize the array  for i:= 0 to high(group) do    group[i] := i * 10;  // print the array with 10 elements per line  arrayprint(group,10, 4);  readln;end. Outputs: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i    0   10 20   30   40   50   60   70   80   90 100  110  120  130  140  150  160  170  180  190 200  210  220  230  240  250  260  270  280  290 300  310  320  330  340  350  360  370  380  390 400  410  420  430  440  450  460  470  480  490 500  510  520  530  540  550  560  570  580  590 600  610  620  630  640  650  660  670  680  690 700  710  720  730  740  750  760  770  780  790 800  810  820  830  840  850  860  870  880  890 900  910  920  930  940  950  960  970  980  990Which means I wanted 10 elements per line,  with formatted width  of  4. Now, that looks better as a single line with 100 elements, agreed? The mod instruction compared to 0 simply tells you there is a integer division without remainder. So time to break.... Plz let me know if you understand it now. This function also works for static arrays, because I use a const array of integer as parameter. (that is different from a dynamic array) I made a slight edit to achieve that. ---------------------------------------------------------------------- jamie · 2025-06-14 13:40 study this. 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 Function DisplayIntegerSeries(aIntegerSeries:Array of Integer):String;var  I:Integer;  S:String;Begin  For I in aIntegerSeries do  //return each item as shown in array.   Begin    if Result <> '' Then Result := Result+','; //Optional comma inserted.    WriteStr(S,I:8); // Create a formated output string with 8 tabbed spaces.    Result := Result+S;   end;end;{Test button click in GUI mode,in console mode place between begin and end. }procedure TForm1.Button1Click(Sender: TObject);Vargroup: array of integer = (10,20,30,40,50,60,71,80,90,91);begin  //For GUI mode  Caption := DisplayIntegerSeries(Group); //Comment this like for console mode.  //For console mode  //writeLn(DisplayIntegerSeries);  //Uncomment this line for console mode.end;                                                                          I put notations there in the test Button Click because I do most things in GUI mode but you can do this in your console mode if you wish. Jamie ---------------------------------------------------------------------- Thaddy · 2025-06-14 14:08 I think my proposal is more flexible. Nothing to take away from the effort. Try my example with 20 instead of 10, or 8 or 19? ---------------------------------------------------------------------- aja_ttara · 2025-06-14 16:49 Quote from: Thaddy on June 14, 2025, 02:38:43 pmI will explain: If an array is really really big and you use the in solution, which is not bad as such, you can not break a display line after a given number of writes for a single line. It happens to be that you can use mod to determine if you want to add a linebreak with writeln. Except for the modulo for 1 being 0, so I look ahead (succ()). Even if the array is really large, we can display it neatly in blocks of the number of array elements you want per line. I hope that helps. Will adapt my example in a minute or so. [EDIT] 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 {$ifdef fpc}{$mode objfpc}{$endif}procedure arrayprint(const values:array of integer;BreakAt:Integer; width:integer);var  i : integer; begin  for i := 0 to High(values) do  begin    write(values[i]:width,' ');     if (succ(i) mod BreakAt = 0) and         ( i > 1) and         (i < high(values)) then           writeln;  end;  end;var  group: array of integer;  i:integer;begin  setlength(group,100); //0 to 99  // initialize the array  for i:= 0 to high(group) do    group[i] := i * 10;  // print the array with 10 elements per line  arrayprint(group,10, 4);  readln;end. Outputs: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i    0   10 20   30   40   50   60   70   80   90 100  110  120  130  140  150  160  170  180  190 200  210  220  230  240  250  260  270  280  290 300  310  320  330  340  350  360  370  380  390 400  410  420  430  440  450  460  470  480  490 500  510  520  530  540  550  560  570  580  590 600  610  620  630  640  650  660  670  680  690 700  710  720  730  740  750  760  770  780  790 800  810  820  830  840  850  860  870  880  890 900  910  920  930  940  950  960  970  980  990Which means I wanted 10 elements per line,  with formatted width  of  4. Now, that looks better as a single line with 100 elements, agreed? The mod instruction compared to 0 simply tells you there is a integer division without remainder. So time to break.... Plz let me know if you understand it now. This function also works for static arrays, because I use a const array of integer as parameter. (that is different from a dynamic array) I made a slight edit to achieve that. +1 I studied the code and I think I understand the logic behind it. I couldn't write it, it's beyond my present abilities , but I find it very useful and elegant, thank you! ---------------------------------------------------------------------- Thaddy · 2025-06-14 16:55 Quote from: aja_ttara on June 14, 2025, 06:49:34 pmQuote from: Thaddy on June 14, 2025, 02:38:43 pmI will explain: If an array is really really big and you use the in solution, which is not bad as such, you can not break a display line after a given number of writes for a single line. It happens to be that you can use mod to determine if you want to add a linebreak with writeln. Except for the modulo for 1 being 0, so I look ahead (succ()). Even if the array is really large, we can display it neatly in blocks of the number of array elements you want per line. I hope that helps. Will adapt my example in a minute or so. [EDIT] 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 {$ifdef fpc}{$mode objfpc}{$endif}procedure arrayprint(const values:array of integer;BreakAt:Integer; width:integer);var  i : integer; begin  for i := 0 to High(values) do  begin    write(values[i]:width,' ');     if (succ(i) mod BreakAt = 0) and         ( i > 1) and         (i < high(values)) then           writeln;  end;  end;var  group: array of integer;  i:integer;begin  setlength(group,100); //0 to 99  // initialize the array  for i:= 0 to high(group) do    group[i] := i * 10;  // print the array with 10 elements per line  arrayprint(group,10, 4);  readln;end. Outputs: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i    0   10 20   30   40   50   60   70   80   90 100  110  120  130  140  150  160  170  180  190 200  210  220  230  240  250  260  270  280  290 300  310  320  330  340  350  360  370  380  390 400  410  420  430  440  450  460  470  480  490 500  510  520  530  540  550  560  570  580  590 600  610  620  630  640  650  660  670  680  690 700  710  720  730  740  750  760  770  780  790 800  810  820  830  840  850  860  870  880  890 900  910  920  930  940  950  960  970  980  990Which means I wanted 10 elements per line,  with formatted width  of  4. Now, that looks better as a single line with 100 elements, agreed? The mod instruction compared to 0 simply tells you there is a integer division without remainder. So time to break.... Plz let me know if you understand it now. This function also works for static arrays, because I use a const array of integer as parameter. (that is different from a dynamic array) I made a slight edit to achieve that. +1 I studied the code and I think I understand the logic behind it. I couldn't write it, it's beyond my present abilities , but I find it very useful and elegant, thank you! That's what we are here for. The other contributions were simpler but also good. I focused on display. ---------------------------------------------------------------------- aja_ttara · 2025-06-14 16:57 Quote from: jamie on June 14, 2025, 03:40:47 pmstudy this. 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 Function DisplayIntegerSeries(aIntegerSeries:Array of Integer):String;var  I:Integer;  S:String;Begin  For I in aIntegerSeries do  //return each item as shown in array.   Begin    if Result <> '' Then Result := Result+','; //Optional comma inserted.    WriteStr(S,I:8); // Create a formated output string with 8 tabbed spaces.    Result := Result+S;   end;end;{Test button click in GUI mode,in console mode place between begin and end. }procedure TForm1.Button1Click(Sender: TObject);Vargroup: array of integer = (10,20,30,40,50,60,71,80,90,91);begin  //For GUI mode  Caption := DisplayIntegerSeries(Group); //Comment this like for console mode.  //For console mode  //writeLn(DisplayIntegerSeries);  //Uncomment this line for console mode.end;                                                                          I put notations there in the test Button Click because I do most things in GUI mode but you can do this in your console mode if you wish. Jamie +1 This code is hard for me to read But it's just because of my inability, thank you Jamie!