Recent

Author Topic: iteradoras en pascal  (Read 2595 times)

zelda16bit

  • Full Member
  • ***
  • Posts: 118
iteradoras en pascal
« on: February 20, 2023, 07:26:42 pm »
Me gustaria saber si hay iteradores para recorrer los contenedores de la biblioteca generic.colletion o fgl,algo parecido a lo que tiene c++.

JdeHaan

  • Full Member
  • ***
  • Posts: 118
Re: iteradoras en pascal
« Reply #1 on: February 20, 2023, 10:30:00 pm »
The translation says:
Code: Text  [Select][+][-]
  1. I would like to know if there are iterators to traverse the containers of the generics.collections or fgl library, something similar to what c++ has.

Short answer: yes, for each container type there are iterators (or enumerators).
example

Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2.  
  3. uses Generics.Collections;
  4.  
  5. var
  6.   list: TList<String>;
  7.   item: String;
  8. begin
  9.   list := TList<String>.Create;
  10.   list.add('hello');
  11.   list.add('world');
  12.   for item in list do
  13.     writeln(item);
  14.   list.Free;
  15. end.
  16.  

zelda16bit

  • Full Member
  • ***
  • Posts: 118
Re: iteradoras en pascal
« Reply #2 on: February 21, 2023, 01:42:30 pm »
Yo me refiero ha algo mas especifico como esto.
Code: Pascal  [Select][+][-]
  1.     std::list<int> lista{10,11,12,13,14,15};
  2.  
  3.     std::list<int>::iterator iterador{ lista.begin() };
  4.  
  5.     while(iterador != lista.end()){
  6.         std::cout<<*iterador<<" ";
  7.         ++iterador;
  8.     }
  9.  
« Last Edit: February 21, 2023, 01:45:31 pm by zelda16bit »

TRon

  • Hero Member
  • *****
  • Posts: 2506
Re: iteradoras en pascal
« Reply #3 on: February 21, 2023, 01:46:04 pm »
Sorry, no Espanol.

No veo el uso de iteradoresmsolo veo
which translates for me to:
Quote
I don't see the use of iterator glaze only see

Code: Pascal  [Select][+][-]
  1. {$mode delphi}
  2.  
  3. uses Generics.Collections;
  4.  
  5. var
  6.   list: TList<String>;
  7.   item: String;
  8. begin
  9.   list := TList<String>.Create;
  10.   list.add('hello');
  11.   list.add('world');
  12.   for item in list do
  13.     writeln(item);
  14.   list.Free;
  15. end.
  16.  
I have highlighted the iterator part.
« Last Edit: February 21, 2023, 01:47:51 pm by TRon »

TRon

  • Hero Member
  • *****
  • Posts: 2506
Re: iteradoras en pascal
« Reply #4 on: February 21, 2023, 02:18:44 pm »
Yo me refiero ha algo mas especifico como esto.

Which translates for me to:
Quote
I mean something more specific like this.

The following example is using mode OBJFPC instead of mode DELPHI and should behave similar as you c-code (the enumarator is again highlighted).
Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. {$mode objfpc}
  4.  
  5. uses
  6.   generics.Collections;
  7.  
  8. type
  9.   TIntegerList = specialize TList<integer>;
  10. var
  11.   integers : array of integer = (10,11,12,13,14,15);
  12.   list: TIntegerList;
  13.   item: integer;
  14. begin
  15.   list := TIntegerList.Create;
  16.  
  17.   // quickly add integer items to list
  18.   for item in integers
  19.     do List.Add(item);
  20.  
  21.   // iterate over list and display each item
  22.   for item in list
  23.     do writeln(item);
  24.  
  25.   list.Free;
  26. end.
  27.  

zelda16bit

  • Full Member
  • ***
  • Posts: 118
Re: iteradoras en pascal
« Reply #5 on: February 21, 2023, 03:27:21 pm »
Y si creo una lista de objetos y luego le añado a esa lista 20 objetos,para despues mostrar esos 20 objetos.Y al final ir eliminando uno a uno esos 20 objetos y luego los vuelvo a mostrar para ver el resultado,como seria.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2054
  • Fifty shades of code.
    • Delphi & FreePascal
Re: iteradoras en pascal
« Reply #6 on: February 21, 2023, 03:35:10 pm »
Y si creo una lista de objetos y luego le añado a esa lista 20 objetos,para despues mostrar esos 20 objetos.Y al final ir eliminando uno a uno esos 20 objetos y luego los vuelvo a mostrar para ver el resultado,como seria.
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$IFDEF MSWINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
  3.  
  4. {$mode objfpc}
  5.  
  6. uses
  7.   generics.Collections;
  8.  
  9. type
  10.   TIntegerList = specialize TList<integer>;
  11. var
  12.   integers : array of integer = (10,11,12,13,14,15);
  13.   list: TIntegerList;
  14.   item: integer;
  15.   i: integer;
  16. begin
  17.   list := TIntegerList.Create;
  18.  
  19.   // quickly add integer items to list
  20.   for item in integers
  21.     do List.Add(item);
  22.  
  23.   // iterate over list and display each item
  24.   for item in list
  25.     do writeln(item);
  26.  
  27.   writeln('deleted last 3');
  28.  
  29.   for i := 0 to 2 do
  30.     List.Delete(Pred(List.Count));
  31.  
  32.   for item in list
  33.     do writeln(item);
  34.  
  35.   list.Free;
  36.   {$IFDEF MSWINDOWS}ReadLn; {$ENDIF}
  37. end.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

zelda16bit

  • Full Member
  • ***
  • Posts: 118
Re: iteradoras en pascal
« Reply #7 on: February 21, 2023, 04:35:32 pm »
Y si creo una lista de objetos y luego le añado a esa lista 20 objetos,para despues mostrar esos 20 objetos.Y al final ir eliminando uno a uno esos 20 objetos y luego los vuelvo a mostrar para ver el resultado,como seria.
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$IFDEF MSWINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
  3.  
  4. {$mode objfpc}
  5.  
  6. uses
  7.   generics.Collections;
  8.  
  9. type
  10.   TIntegerList = specialize TList<integer>;
  11. var
  12.   integers : array of integer = (10,11,12,13,14,15);
  13.   list: TIntegerList;
  14.   item: integer;
  15.   i: integer;
  16. begin
  17.   list := TIntegerList.Create;
  18.  
  19.   // quickly add integer items to list
  20.   for item in integers
  21.     do List.Add(item);
  22.  
  23.   // iterate over list and display each item
  24.   for item in list
  25.     do writeln(item);
  26.  
  27.   writeln('deleted last 3');
  28.  
  29.   for i := 0 to 2 do
  30.     List.Delete(Pred(List.Count));
  31.  
  32.   for item in list
  33.     do writeln(item);
  34.  
  35.   list.Free;
  36.   {$IFDEF MSWINDOWS}ReadLn; {$ENDIF}
  37. end.

Como seria con una clase.
Code: Pascal  [Select][+][-]
  1. type
  2.   Tclase = class
  3.     numero: integer;
  4.     numero2: integer;
  5.     property propiedad: integer read numero2 write numero2;
  6.     procedure metodo();
  7.   end;
  8.  
  9. procedure Tclase.metodo();
  10. begin
  11.   writeln('el numero es: ',numero+10);
  12. end;
  13.  

TRon

  • Hero Member
  • *****
  • Posts: 2506
Re: iteradoras en pascal
« Reply #8 on: February 22, 2023, 08:57:41 am »
@KodeZwerg: Thank you !

@zelda16bit:
your question translates for me to
Quote
As it would be with a class.
In combination with your code the remark that you made does not make any sense to me so I have no idea what exactly you are asking for or are referring to.

Translated with an online translator to Spanish:
Quote
En combinación con su código, el comentario que hizo no tiene ningún sentido para mí, así que no tengo idea de qué es exactamente lo que está pidiendo o a lo que se refiere.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2054
  • Fifty shades of code.
    • Delphi & FreePascal
Re: iteradoras en pascal
« Reply #9 on: February 22, 2023, 11:45:04 am »
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$IFDEF MSWINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
  3.  
  4. {$mode objfpc}
  5.  
  6. uses
  7.   Generics.Collections;
  8.  
  9. type
  10.  
  11.   { TCustomList }
  12.   TIntegerList = specialize TList<integer>;
  13.   TCustomList = class(TObject)
  14.     strict private
  15.       FCount: Integer;
  16.       FIndex: Integer;
  17.       FCurrent: Integer;
  18.       FList: TIntegerList;
  19.     private
  20.       procedure SetCount(const AValue: Integer);
  21.       procedure SetIndex(const AValue: Integer);
  22.       function GetCurrent: Integer;
  23.       procedure SetCurrent(const AValue: Integer);
  24.     public
  25.       constructor Create;
  26.       destructor Destroy; override;
  27.       procedure Add(const AValue: Integer);
  28.       procedure Show;
  29.       procedure Remove(const AIndex: Integer);
  30.     published
  31.       property Count: Integer read FCount write SetCount default 0;
  32.       property Index: Integer read FIndex write SetIndex default -1;
  33.       property Current: Integer read GetCurrent write SetCurrent default -1;
  34.   end;
  35.  
  36. { begin TCustomList }
  37. constructor TCustomList.Create;
  38. begin
  39.   inherited Create;
  40.   FList := TIntegerList.Create;
  41.   FCount := 0;
  42.   FIndex := -1;
  43.   FCurrent := -1;
  44. end;
  45.  
  46. destructor TCustomList.Destroy;
  47. begin
  48.   FList.Free;
  49.   FList := nil;
  50.   inherited Destroy;
  51. end;
  52.  
  53. procedure TCustomList.SetIndex(const AValue: Integer);
  54. begin
  55.   if ((AValue >= 0) and (AValue < FCount)) then
  56.     FIndex := AValue
  57.     else
  58.     FIndex := -1;
  59. end;
  60.  
  61. function TCustomList.GetCurrent: Integer;
  62. begin
  63.   if ((FIndex >= 0) and (FIndex < FCount)) then
  64.     Result := FList.Items[FIndex]
  65.     else
  66.     Result := -1;
  67. end;
  68.  
  69. procedure TCustomList.SetCurrent(const AValue: Integer);
  70. begin
  71.   if ((FIndex >= 0) and (FIndex < FCount)) then
  72.     FList.Items[FIndex] := AValue;
  73. end;
  74.  
  75. procedure TCustomList.SetCount(const AValue: Integer);
  76. var
  77.   i: Integer;
  78. begin
  79.   if (FCount = 0) then
  80.     Exit;
  81.   if (AValue < FCount) then
  82.     for i := Pred(FCount) downto AValue do
  83.       FList.Delete(i);
  84.   FCount := FList.Count;
  85.   FIndex := Pred(FCount);
  86. end;
  87.  
  88. procedure TCustomList.Add(const AValue: Integer);
  89. begin
  90.   FList.Add(AValue);
  91.   FCount := FList.Count;
  92.   FIndex := Pred(FCount);
  93. end;
  94.  
  95. procedure TCustomList.Remove(const AIndex: Integer);
  96. begin
  97.   if (FCount = 0) then
  98.     Exit;
  99.   if ((AIndex >= 0) and (AIndex < FCount)) then
  100.     FList.Delete(AIndex);
  101.   FCount := FList.Count;
  102.   FIndex := Pred(FCount);
  103. end;
  104.  
  105. procedure TCustomList.Show;
  106. var
  107.   i: Integer;
  108. begin
  109.   if (FCount = 0) then
  110.     Exit;
  111.   for i in FList
  112.     do WriteLn(i);
  113. end;
  114. { end TCustomList }
  115.  
  116. var
  117.   List: TCustomList;
  118.   i: integer;
  119. begin
  120.   List := TCustomList.Create;
  121.   try
  122.     WriteLn('Add 10 elements');
  123.     for i := 0 to 9 do
  124.       List.Add(i);
  125.     WriteLn('Show all elements');
  126.     List.Show;
  127.     WriteLn('Remove last 5 elements');
  128.     List.Count := List.Count - 5;
  129.     WriteLn('Show all elements');
  130.     List.Show;
  131.     WriteLn('Change value of element 3 to 99');
  132.     List.Index := 2;
  133.     List.Current := 99;
  134.     WriteLn('Show all elements');
  135.     List.Show;
  136.   finally
  137.     List.Free;
  138.   end;
  139.   {$IFDEF MSWINDOWS}ReadLn; {$ENDIF}
  140. end.
¿Supongo que querías esta lista en una clase?
Quote
I assume you wanted this list in a class?
« Last Edit: February 22, 2023, 12:37:09 pm by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

zelda16bit

  • Full Member
  • ***
  • Posts: 118
Re: iteradoras en pascal
« Reply #10 on: February 22, 2023, 01:56:03 pm »
@KodeZwerg: Thank you !

@zelda16bit:
your question translates for me to
Quote
As it would be with a class.
In combination with your code the remark that you made does not make any sense to me so I have no idea what exactly you are asking for or are referring to.

Translated with an online translator to Spanish:
Quote
En combinación con su código, el comentario que hizo no tiene ningún sentido para mí, así que no tengo idea de qué es exactamente lo que está pidiendo o a lo que se refiere.

Lo que estaba preguntando es que en vez de crear una lista de enteros,para añadir datos enteros se creara una lista de objetos,y ver como añadir esos objetos a la lista, y luego ver como se itera sobre esa lista de objetos.
Por eso mostraba una clase hipotetica para añadir a esa lista,pero mi respuesta del principio a quedado respondida ya que veo que en pascal no hace falta ningun iterador especial para recorrer la lista,con el bucle "for in" es suficiente,al menos es la conclusion que saco al ver los ejemplos mostrados.Gracias  :)
« Last Edit: February 22, 2023, 01:59:43 pm by zelda16bit »

zelda16bit

  • Full Member
  • ***
  • Posts: 118
Re: iteradoras en pascal
« Reply #11 on: February 22, 2023, 01:59:09 pm »
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$IFDEF MSWINDOWS}{$APPTYPE CONSOLE}{$ENDIF}
  3.  
  4. {$mode objfpc}
  5.  
  6. uses
  7.   Generics.Collections;
  8.  
  9. type
  10.  
  11.   { TCustomList }
  12.   TIntegerList = specialize TList<integer>;
  13.   TCustomList = class(TObject)
  14.     strict private
  15.       FCount: Integer;
  16.       FIndex: Integer;
  17.       FCurrent: Integer;
  18.       FList: TIntegerList;
  19.     private
  20.       procedure SetCount(const AValue: Integer);
  21.       procedure SetIndex(const AValue: Integer);
  22.       function GetCurrent: Integer;
  23.       procedure SetCurrent(const AValue: Integer);
  24.     public
  25.       constructor Create;
  26.       destructor Destroy; override;
  27.       procedure Add(const AValue: Integer);
  28.       procedure Show;
  29.       procedure Remove(const AIndex: Integer);
  30.     published
  31.       property Count: Integer read FCount write SetCount default 0;
  32.       property Index: Integer read FIndex write SetIndex default -1;
  33.       property Current: Integer read GetCurrent write SetCurrent default -1;
  34.   end;
  35.  
  36. { begin TCustomList }
  37. constructor TCustomList.Create;
  38. begin
  39.   inherited Create;
  40.   FList := TIntegerList.Create;
  41.   FCount := 0;
  42.   FIndex := -1;
  43.   FCurrent := -1;
  44. end;
  45.  
  46. destructor TCustomList.Destroy;
  47. begin
  48.   FList.Free;
  49.   FList := nil;
  50.   inherited Destroy;
  51. end;
  52.  
  53. procedure TCustomList.SetIndex(const AValue: Integer);
  54. begin
  55.   if ((AValue >= 0) and (AValue < FCount)) then
  56.     FIndex := AValue
  57.     else
  58.     FIndex := -1;
  59. end;
  60.  
  61. function TCustomList.GetCurrent: Integer;
  62. begin
  63.   if ((FIndex >= 0) and (FIndex < FCount)) then
  64.     Result := FList.Items[FIndex]
  65.     else
  66.     Result := -1;
  67. end;
  68.  
  69. procedure TCustomList.SetCurrent(const AValue: Integer);
  70. begin
  71.   if ((FIndex >= 0) and (FIndex < FCount)) then
  72.     FList.Items[FIndex] := AValue;
  73. end;
  74.  
  75. procedure TCustomList.SetCount(const AValue: Integer);
  76. var
  77.   i: Integer;
  78. begin
  79.   if (FCount = 0) then
  80.     Exit;
  81.   if (AValue < FCount) then
  82.     for i := Pred(FCount) downto AValue do
  83.       FList.Delete(i);
  84.   FCount := FList.Count;
  85.   FIndex := Pred(FCount);
  86. end;
  87.  
  88. procedure TCustomList.Add(const AValue: Integer);
  89. begin
  90.   FList.Add(AValue);
  91.   FCount := FList.Count;
  92.   FIndex := Pred(FCount);
  93. end;
  94.  
  95. procedure TCustomList.Remove(const AIndex: Integer);
  96. begin
  97.   if (FCount = 0) then
  98.     Exit;
  99.   if ((AIndex >= 0) and (AIndex < FCount)) then
  100.     FList.Delete(AIndex);
  101.   FCount := FList.Count;
  102.   FIndex := Pred(FCount);
  103. end;
  104.  
  105. procedure TCustomList.Show;
  106. var
  107.   i: Integer;
  108. begin
  109.   if (FCount = 0) then
  110.     Exit;
  111.   for i in FList
  112.     do WriteLn(i);
  113. end;
  114. { end TCustomList }
  115.  
  116. var
  117.   List: TCustomList;
  118.   i: integer;
  119. begin
  120.   List := TCustomList.Create;
  121.   try
  122.     WriteLn('Add 10 elements');
  123.     for i := 0 to 9 do
  124.       List.Add(i);
  125.     WriteLn('Show all elements');
  126.     List.Show;
  127.     WriteLn('Remove last 5 elements');
  128.     List.Count := List.Count - 5;
  129.     WriteLn('Show all elements');
  130.     List.Show;
  131.     WriteLn('Change value of element 3 to 99');
  132.     List.Index := 2;
  133.     List.Current := 99;
  134.     WriteLn('Show all elements');
  135.     List.Show;
  136.   finally
  137.     List.Free;
  138.   end;
  139.   {$IFDEF MSWINDOWS}ReadLn; {$ENDIF}
  140. end.
¿Supongo que querías esta lista en una clase?
Quote
I assume you wanted this list in a class?

No es exactamente lo que estaba preguntando y nunca habia hecho este tipo de codigo pero me sirve para aprender algo nuevo,no pregunto mas porque se esta liando mucho este hilo y se esta alejando de la pregunta inicial.Gracias por la ayuda  :)

 

TinyPortal © 2005-2018