Recent

Author Topic: show the content of an array  (Read 597 times)

aja_ttara

  • New Member
  • *
  • Posts: 10
show the content of an array
« on: June 14, 2025, 12:39:50 am »
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

  • Sr. Member
  • ****
  • Posts: 370
Re: show the content of an array
« Reply #1 on: June 14, 2025, 02:09:19 am »
The cleanest way to iterate through the array - which I believe you need to do - is something like:
Code: Pascal  [Select][+][-]
  1. var
  2.   i : integer;
  3.   group: array of integer = (10,20,30,40,50,60,71,80,90,91);
  4. begin
  5.   for i in group do
  6.     write(i,' ');
  7.   readln();
  8. end.
  9.  

A alternative would be to create a procedure that takes the array as a parameter, then writes the array content...

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Thaddy

  • Hero Member
  • *****
  • Posts: 17476
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: show the content of an array
« Reply #2 on: June 14, 2025, 02:10:14 pm »
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][+][-]
  1. {$ifdef fpc}{$mode objfpc}{$endif}
  2. uses types;
  3. procedure arrayprint(const values:TIntegerDynArray;BreakAt:Integer);
  4. var
  5.   i : integer;
  6.  begin
  7.   for i := 0 to High(values) do
  8.   begin
  9.     write(values[i],' ');
  10.      if (succ(i) mod BreakAt = 0) and
  11.         ( i > 1) and
  12.         (i < high(values)) then
  13.           writeln;
  14.   end;
  15.  
  16. end;
  17.  
  18. var
  19.   group: array of integer = (10,20,30,40,50,60,71,80,90,91);
  20. begin
  21.   arrayprint(group,3);
  22. end.
This prevents large arrays being printed on a single line.
« Last Edit: June 14, 2025, 02:28:13 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

aja_ttara

  • New Member
  • *
  • Posts: 10
Re: show the content of an array
« Reply #3 on: June 14, 2025, 02:12:55 pm »
The cleanest way to iterate through the array - which I believe you need to do - is something like:
Code: Pascal  [Select][+][-]
  1. var
  2.   i : integer;
  3.   group: array of integer = (10,20,30,40,50,60,71,80,90,91);
  4. begin
  5.   for i in group do
  6.     write(i,' ');
  7.   readln();
  8. end.
  9.  

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

  • Hero Member
  • *****
  • Posts: 17476
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: show the content of an array
« Reply #4 on: June 14, 2025, 02:23:38 pm »
Posts crossed but better look at my answer.
The use of in is not appropriate here, because you can not break at the index.
« Last Edit: June 14, 2025, 02:33:21 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

aja_ttara

  • New Member
  • *
  • Posts: 10
Re: show the content of an array
« Reply #5 on: June 14, 2025, 02:33:15 pm »
Posts 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

  • Hero Member
  • *****
  • Posts: 17476
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: show the content of an array
« Reply #6 on: June 14, 2025, 02:38:43 pm »
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][+][-]
  1. {$ifdef fpc}{$mode objfpc}{$endif}
  2. procedure arrayprint(const values:array of integer;BreakAt:Integer; width:integer);
  3. var
  4.   i : integer;
  5.  begin
  6.   for i := 0 to High(values) do
  7.   begin
  8.     write(values[i]:width,' ');
  9.      if (succ(i) mod BreakAt = 0) and
  10.         ( i > 1) and
  11.         (i < high(values)) then
  12.           writeln;
  13.   end;
  14.  
  15. end;
  16. var
  17.   group: array of integer;
  18.   i:integer;
  19. begin
  20.   setlength(group,100); //0 to 99
  21.   // initialize the array
  22.   for i:= 0 to high(group) do
  23.     group[i] := i * 10;
  24.   // print the array with 10 elements per line
  25.   arrayprint(group,10, 4);
  26.   readln;
  27. end.

Outputs:
Code: Text  [Select][+][-]
  1.    0   10   20   30   40   50   60   70   80   90
  2.  100  110  120  130  140  150  160  170  180  190
  3.  200  210  220  230  240  250  260  270  280  290
  4.  300  310  320  330  340  350  360  370  380  390
  5.  400  410  420  430  440  450  460  470  480  490
  6.  500  510  520  530  540  550  560  570  580  590
  7.  600  610  620  630  640  650  660  670  680  690
  8.  700  710  720  730  740  750  760  770  780  790
  9.  800  810  820  830  840  850  860  870  880  890
  10.  900  910  920  930  940  950  960  970  980  990
Which 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.
« Last Edit: June 14, 2025, 03:09:55 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

jamie

  • Hero Member
  • *****
  • Posts: 6995
Re: show the content of an array
« Reply #7 on: June 14, 2025, 03:40:47 pm »
study this.
Code: Pascal  [Select][+][-]
  1. Function DisplayIntegerSeries(aIntegerSeries:Array of Integer):String;
  2. var
  3.   I:Integer;
  4.   S:String;
  5. Begin
  6.   For I in aIntegerSeries do  //return each item as shown in array.
  7.    Begin
  8.     if Result <> '' Then Result := Result+','; //Optional comma inserted.
  9.     WriteStr(S,I:8); // Create a formated output string with 8 tabbed spaces.
  10.     Result := Result+S;
  11.    end;
  12. end;
  13. {Test button click in GUI mode,in console mode place between begin and end. }
  14. procedure TForm1.Button1Click(Sender: TObject);
  15. Var
  16. group: array of integer = (10,20,30,40,50,60,71,80,90,91);
  17. begin
  18.   //For GUI mode
  19.   Caption := DisplayIntegerSeries(Group); //Comment this like for console mode.
  20.   //For console mode
  21.   //writeLn(DisplayIntegerSeries);  //Uncomment this line for console mode.
  22. end;                                                                          
  23.  
  24.  
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
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 17476
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: show the content of an array
« Reply #8 on: June 14, 2025, 04:08:52 pm »
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?
« Last Edit: June 14, 2025, 06:52:01 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

aja_ttara

  • New Member
  • *
  • Posts: 10
Re: show the content of an array
« Reply #9 on: June 14, 2025, 06:49:34 pm »
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][+][-]
  1. {$ifdef fpc}{$mode objfpc}{$endif}
  2. procedure arrayprint(const values:array of integer;BreakAt:Integer; width:integer);
  3. var
  4.   i : integer;
  5.  begin
  6.   for i := 0 to High(values) do
  7.   begin
  8.     write(values[i]:width,' ');
  9.      if (succ(i) mod BreakAt = 0) and
  10.         ( i > 1) and
  11.         (i < high(values)) then
  12.           writeln;
  13.   end;
  14.  
  15. end;
  16. var
  17.   group: array of integer;
  18.   i:integer;
  19. begin
  20.   setlength(group,100); //0 to 99
  21.   // initialize the array
  22.   for i:= 0 to high(group) do
  23.     group[i] := i * 10;
  24.   // print the array with 10 elements per line
  25.   arrayprint(group,10, 4);
  26.   readln;
  27. end.

Outputs:
Code: Text  [Select][+][-]
  1.    0   10   20   30   40   50   60   70   80   90
  2.  100  110  120  130  140  150  160  170  180  190
  3.  200  210  220  230  240  250  260  270  280  290
  4.  300  310  320  330  340  350  360  370  380  390
  5.  400  410  420  430  440  450  460  470  480  490
  6.  500  510  520  530  540  550  560  570  580  590
  7.  600  610  620  630  640  650  660  670  680  690
  8.  700  710  720  730  740  750  760  770  780  790
  9.  800  810  820  830  840  850  860  870  880  890
  10.  900  910  920  930  940  950  960  970  980  990
Which 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

  • Hero Member
  • *****
  • Posts: 17476
  • Ceterum censeo Trumpum esse delendum (Tnx Charlie)
Re: show the content of an array
« Reply #10 on: June 14, 2025, 06:55:07 pm »
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][+][-]
  1. {$ifdef fpc}{$mode objfpc}{$endif}
  2. procedure arrayprint(const values:array of integer;BreakAt:Integer; width:integer);
  3. var
  4.   i : integer;
  5.  begin
  6.   for i := 0 to High(values) do
  7.   begin
  8.     write(values[i]:width,' ');
  9.      if (succ(i) mod BreakAt = 0) and
  10.         ( i > 1) and
  11.         (i < high(values)) then
  12.           writeln;
  13.   end;
  14.  
  15. end;
  16. var
  17.   group: array of integer;
  18.   i:integer;
  19. begin
  20.   setlength(group,100); //0 to 99
  21.   // initialize the array
  22.   for i:= 0 to high(group) do
  23.     group[i] := i * 10;
  24.   // print the array with 10 elements per line
  25.   arrayprint(group,10, 4);
  26.   readln;
  27. end.

Outputs:
Code: Text  [Select][+][-]
  1.    0   10   20   30   40   50   60   70   80   90
  2.  100  110  120  130  140  150  160  170  180  190
  3.  200  210  220  230  240  250  260  270  280  290
  4.  300  310  320  330  340  350  360  370  380  390
  5.  400  410  420  430  440  450  460  470  480  490
  6.  500  510  520  530  540  550  560  570  580  590
  7.  600  610  620  630  640  650  660  670  680  690
  8.  700  710  720  730  740  750  760  770  780  790
  9.  800  810  820  830  840  850  860  870  880  890
  10.  900  910  920  930  940  950  960  970  980  990
Which 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.
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

aja_ttara

  • New Member
  • *
  • Posts: 10
Re: show the content of an array
« Reply #11 on: June 14, 2025, 06:57:50 pm »
study this.
Code: Pascal  [Select][+][-]
  1. Function DisplayIntegerSeries(aIntegerSeries:Array of Integer):String;
  2. var
  3.   I:Integer;
  4.   S:String;
  5. Begin
  6.   For I in aIntegerSeries do  //return each item as shown in array.
  7.    Begin
  8.     if Result <> '' Then Result := Result+','; //Optional comma inserted.
  9.     WriteStr(S,I:8); // Create a formated output string with 8 tabbed spaces.
  10.     Result := Result+S;
  11.    end;
  12. end;
  13. {Test button click in GUI mode,in console mode place between begin and end. }
  14. procedure TForm1.Button1Click(Sender: TObject);
  15. Var
  16. group: array of integer = (10,20,30,40,50,60,71,80,90,91);
  17. begin
  18.   //For GUI mode
  19.   Caption := DisplayIntegerSeries(Group); //Comment this like for console mode.
  20.   //For console mode
  21.   //writeLn(DisplayIntegerSeries);  //Uncomment this line for console mode.
  22. end;                                                                          
  23.  
  24.  
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!

 

TinyPortal © 2005-2018