How to Copy Different Length Arrays To Another One? [Edit]

I have a routine that uses elements of different length arrays.
Let's say:
{code}
List1 : Array[1..1] Of String = ('A');
List2 : Array[1..4] Of String = ('B', 'C', D', 'E');
List3 : Array[1..3] Of String = ('F', 'G', H');
{code}
I wish I could simply copy the content of each array to a common one according to my needs, instead of having to write different procedures for each List1, List2 and List3.
Something like "*If xxx then List = List1 else List = List2*". And then use:
{code}
begin
  for i := 1 to Length(List) do
    With Query1 do
      begin
        ParamByName('Code').AsString := List[i];
        Open;
      end; 
end;
{code}
If all three arrays had the same length, I know I could create List[] the same size. But that's not the case.
I've tried:
{code}
If xxx then
  begin
    SetLength(List,Length(List3));
    for i:=Low(List3) to High(List3) do
      List[i]:=List3[i];
  end;
{code}
But I got Incompatible types. I'm quite lost now. :)

Thanks,
Marcio
0
Marcio
7/21/2011 11:21:54 PM
embarcadero.delphi.general 4258 articles. 0 followers. Follow

4 Replies
399 Views

Similar Articles

[PageSpeed] 42

I tried this to define the common array and, altough not getting a compile error as before, the loop is not exiting correctly and now I'm getting an access violation after the last value:

{code}
var
  List : Array of String;
begin
If xxx then
  begin
    SetLength(List,Length(List3));
    for i:=Low(List3) to High(List3) do
      List[i]:=List3[i];
  end;
end;
{code}

Any other idea?
Marcio
0
Marcio
7/22/2011 12:51:39 AM
Marcio Ehrlich wrote:

> I tried this to define the common array and, altough not getting a compile error as before, the
> loop is not exiting correctly and now I'm getting an access violation after the last value:
> 
> {code}
> var
>   List : Array of String;
> begin
> If xxx then
>   begin
>     SetLength(List,Length(List3));
>     for i:=Low(List3) to High(List3) do
>       List[i]:=List3[i];
>   end;
> end;
> {code}

Marcio, List is a dynamic array and the indices of dynamic array are always zero based.
Since the static arrays you have defined, List1, List2 and List3 are indexed one based, the
assignments to List are offset by one.

This should work:
{code}
    SetLength(List,Length(List3));
    for i:=Low(List3) to High(List3) do
      List[i-1]:=List3[i];
{code}

Cheers
Tom

-- 
Tom Brunberg
0
Tom
7/22/2011 1:05:23 AM
Absolutely fantastic.
Thanks a lot, Tom!!
M.

> {quote:title=Tom Brunberg wrote:}{quote}

> This should work:
> {code}
>     SetLength(List,Length(List3));
>     for i:=Low(List3) to High(List3) do
>       List[i-1]:=List3[i];
> {code}
0
Marcio
7/22/2011 1:24:41 AM
Hello Marcio,

you could also write a routine taking an "open array" like this
{code}
procedure DoSomething(const AList: array of string);
var
  i: Integer;
begin
  for i := Low(AList) to High(AList) do
    // ...
end;
{code}
and call it like this:
{code}
case xxx of
  1: DoSomething(List1);
  2: DoSomething(List2);
  3: DoSomething(List3);
end;
{code}
Then you have no need to _copy_ the arrays at all. What Tom wrote about one and zero based arrays still applies. You might wanrt to read http://rvelthuis.de/articles/articles-openarr.html regarding the differences between dynamic and open arrays.

HTH,
Uli.
0
Ulrich
7/22/2011 6:54:47 AM
Reply:

Similar Artilces:

how to copy one table from one database to another on different servers?
 Hello. I need to copy all of the rows in a table from a database on one server, to another existing table of the same name in a different database on a different server.  I'm trying to use a SELECT INTO statement.  Any idea how to do this?I've tried SELECT          *   INTO                  DestinationServer.dbo.DestinationDB.DestinationTableFROM             &nb...

Copying information from One table to Another from 2 different databases on one server
Can I copy accross databases e.g.: In MS SQL I can: Insert into Database2..customer select * from Database1..customer Thanks in Advance Yes you can. The syntax in Sybase is same. The syntax is the same if you are on ASE. ASA does not support that syntax. You need to use a Proxy table instead. -- Jim Egan [TeamSybase] Houston, TX http://www.eganomics.com Sybase Developers Network http://sdn.sybase.com/sdn/mec/mec_home.stm ...

Copying a 2D array to another one
Hello, I have an 2D array like this MyArray:array of array of boolean; what is the fastest way to copy all the datas from this array to another 2D array of same size ? Thanks John A Move with a SizeOf ? DH how do you use it with a 2D array ? for _each := Low(source) to High(source) do move(source[_each], _target[_each], Length(source)*sizeof(boolean)); That only works if the array sizes are the same and if the arrays are already allocated ! DH John Terry wrote: > Hello, > > I have an 2D array like this MyArray:array of array of boolean; ...

Copying a TMenuItem from one menu to another [Edit]
Hi I need to copy several instances of a TMenuItem from a popup menu, which is dynamically populated, to a TMenuItem. Due to the fact that the menu item instance is used in a TDictionary as its Key parameter I need to copy the actual instance rather than simply cloning the MenuItem. i.e. DestinationMenu.Add(SourcePopupMenu.Items[i]); not DestinationMenu.Add(SourcePopupMenu.Items[i].Clone); Unfortunately changing the code that populates the TPopupMenu to be able to populate a TMenuItem would not necessarily be practical due to deadlines and the complexity of the code. I am us...

how to set array length with delphi 2010 rtti [Edit]
how to set array length in runtime ? setLength(t.GetProperty('Propertys'),3); ???? {code:delphi} unit Unit3; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TSubProperty = Class private Fitem2: Integer; Fitem1: String; procedure Setitem1(const Value: String); procedure Setitem2(const Value: Integer); published property item1:String read Fitem1 write Setitem1; property item2:Integer read Fitem2 write Setitem2;...

Delphi 7 Copy Array of Byte to Variant [Edit]
Hello I'm using Delphi 7 and need to copy an array of byte to a variant. The use case is to copy a file on disk to a variant. What I doing now is shown below, but it would seem that the start of the file is not transferred to the variant correctly: The buffer which contains the file would seem to be ok but there is something wrong with the variant. Thanks in advance for any help. Paul var afile: file of byte; buffer: array of byte; i: Integer; vv : variant; begin // read the file into an array of byte AssignFile(afile, LocalPath+LocalFile...

Copy an array to another array by Pointer
Hi All, I have a lot of arrays like SomeArray1: Array[1..NumberOfElements1] of String = ('element1', 'element2', 'element3', 'element4' ....... SomeArray2: Array[1..NumberOfElements2] of String = ('element1', 'element2', 'element3', 'element4' ....... SomeArray3: Array[1..NumberOfElements3] of String = ('element1', 'element2', 'element3', 'element4' ....... SomeArray4: Array[1..NumberOfElements4] of String = ('element1', 'element2', 'element3', 'element4&...

Howto copy TTable fields from one DB to another [Edit]
I have a TTable with 8 fields where two are lookup fields. I then need to copy all fields to a new TTable instance. But when read FieldDefs.Count then this shows the number of fields excluding the lookup fields. Fields.Count show the correct number, but I can't figure out how to add TField to Fields. Does anyone have some suggestion on what method to use if I want to copy the entire database structure to a new instance? Edited by: Mats Webjorn on Feb 7, 2013 9:02 AM "Mats Webjorn" wrote in message news:545465@forums.embarcadero.com... >I have a TTable with 8 fields wher...

Delphi 2010 RTTI Set length dynamic array [Edit]
Good day, I have a problem, i had searched this on google, and EDN but i didn't find a good source, or complet. var x,y:TValue; a:array [0..100] of integer; begin x:=TValue.From(v); y:=TValue.From(a); y.SetArrayElement(0,2); y.SetArrayElement(1,2); end; It works. because the compiler had allocated 100*32 bits in the heap memory. How i will procede for a dynamic array a:array of integer? I want: var y:TValue; a:array of integer; begin y:=TValue.From(a); y.SetArrayElement(y.GetArrayLength+1); Thank you very much and have a nice day. ...

Delphi XE6 FireDAC SQLite copy a table from one database to another
How do I copy a single table from one SQLite database to another? Is there a simple command to do this? Thanks! > How do I copy a single table from one SQLite database to another? Is there a simple command to do this? Thanks! You should use ATTACH command. For example: {code} ATTACH 'c:\secondDB.sdb' AS sec; INSERT INTO SomeTab SELECT * FROM sec.SomeTab; {code} -- With best regards, Dmitry Arefiev / FireDAC Architect Thanks! > {quote:title=Dmitry Arefiev wrote:}{quote} > > How do I copy a single table from one SQLite database to another? Is there a...

Copy a complex project from one project group to another project group? [Edit]
Hi! I have a project group with several projects, where each project contains many different files. What is the most easy, quick and secure (i.e. semi-automatic) method to copy a single project from this project group to another project group? Peter Panino wrote: > Hi! I have a project group with several projects, where each project contains many different files. What is the most easy, quick and secure (i.e. semi-automatic) method to copy a single project from this project group to another project group? Using "add existing project" comes to mind... No, "Add existing ...

Different Name for one email list and one for another?
If I subscribe to an email list as one Identity and to another as a different Identity, when I write TO that list, my Thunderbird Server/Account name is the actual one that gets transmitted ... For whatever reason, I choose to perhaps be called "Schaeffer Hund" on a doggie list and "Frobozz of Zork" on an adventure-game list ... in some other email clients I have use, The Bat! for example, one can assign an Identity and other parameters to such situations ... is there such a way using Thunderbird? -- Frobozz der HirtenHundSchaefferpferdglock Macbook Pro 2...

Validate one datagrid edit field off another edit field
ok I have a dropdownlist if the user selects Item 3 then I want to show a requiredfieldvalidator for the datefield. see your previous post. I can provide you with some sample code doing not exactly that but using one control validation based on the value of the other one. Give me a shout if you are interested Cheers Bug [MCSD] i did not think the first one went through, thanks I will look...

copying data from one table to a new one with some different data types
Is it possible to easily copy data from one table to another if the data types don't match.   I know you can do a INSERT INTO table1(col1,col2)  SELECT (col2,col7) FROM table2 if the data types match but is there a way to do this if they don't. I'm not trying to copy date times into bit fields or anything.  I just have an old table that I built when I really didn't know what I was doing now I at leastthink I have a better understanding of what data types to use, so I was wanting to move the data in the orignal table to my new one.  Most of the fields in the oldd...

Web resources about - How to Copy Different Length Arrays To Another One? [Edit] - embarcadero.delphi.general

Bernie Sanders: Hillary Clinton’s first hurdle in 2016 presidential race
Bernie Sanders, a self-proclaimed democratic socialist, has emerged as an alternative choice for Democrat voters in the presidential race.

Fake bomb on Air France plane looked like clock
Thought experiment: If Ahmed's "clock" were found on the plane, what would pilots have done?

Israel hit by rockets from Lebanon as tensions rise after Hezbollah's Samir Kantar killing
Katyusha missiles fired into Israel after air strike killed Hezbollah leader in Syria.

China landslide leaves 59 missing, sparks gas explosion — Xinhua
Jamaica Observer China landslide leaves 59 missing, sparks gas explosion — Xinhua Jamaica Observer Rescuers look for survivors after a landslide ...

Exit Polls: Strong Showing in Spain Vote by Upstart Parties
Washington Post Exit Polls: Strong Showing in Spain Vote by Upstart Parties New York Times MADRID — A strong showing by a pair of upstart ...

NYC mayor, police boss mark year since 2 officers were slain
Albuquerque’s source for local news and live streaming video online. KRQE reports on news, weather, traffic, investigations and sports for New ...

Nirbhaya gang rape: Delhi HC refuses to stay release of juvenile convict
The juvenile convicted in the 2012 Delhi gangrape case will walk free on Sunday after the Delhi high court on Friday refused to pass any directive ...

Martin Shkreli Explains Why He Jacked the Price of AIDS Drug Daraprim in New Livestream: WATCH
Former Turing Pharmaceuticals CEO Martin Shkreli has returned to live-streaming following his arrest last week. A listener asked him why he jacked ...

12 Days of Christmas Presents Past: HereHave a Brita Filtration Pitcher
As a sort of advent calendar of kitsch, Pictorial will be counting down the final twelve days before Christmas by featuring some holiday presents ...

British women soldiers to go into close combat 'within months', says David Cameron
Prime Minister David Cameron has asked the Ministry of Defence to prepare women for the frontline by the end of 2016.

Resources last updated: 12/20/2015 9:28:45 PM