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' .......
SomeArray5: Array[1..NumberOfElements5] of String = ('element1', 'element2', 
'element3', 'element4' .......

I have a lot of If statements copying one array to another but I want to 
eliminate every copy part in every If statement
This is what I currently have

If SomeCondition = 'SomeArray1'
  Then Begin
             SetLength(NewArray, NumberOfElements1 +1);
             For Counter := 1 to NumberOfElements1 do
                Begin
                  NewArray[Counter] := SomeArray1[Counter];
                End;
           End;

If SomeCondition = 'SomeArray2'
  Then Begin
             SetLength(NewArray, NumberOfElements2 +1);
             For Counter := 1 to NumberOfElements2 do
                Begin
                  NewArray[Counter] := SomeArray2[Counter];
                End;
           End;

and so one

I want to remove every copying parts and replace it like this:

If SomeCondition = 'SomeArray1'
  Then Begin
             NumberOfElements := NumberOfElements1;
             Pointer to SomeArray1
           End;

If SomeCondition = 'SomeArray2'
  Then Begin
             NumberOfElements := NumberOfElements2;
             Pointer to SomeArray2
           End;

and than having only one copy part:

SetLength(NewArray, NumberOfElements +1);
For Counter := 1 to NumberOfElements do
  Begin
     NewArray[Counter] := Get data from Pointer
  End;

Is this possible?

Thanks in advance.

Peter
0
Peter
6/26/2009 5:03:48 PM
embarcadero.delphi.general 4258 articles. 0 followers. Follow

2 Replies
663 Views

Similar Articles

[PageSpeed] 33

Peter Herijgers wrote:

> Hi All,
> 
> I have a lot of arrays like
> 
>  : 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' .......
> SomeArray5: Array[1..NumberOfElements5] of String = ('element1',
> 'element2', 'element3', 'element4' .......
> 
> I have a lot of If statements copying one array to another but I want
> to eliminate every copy part in every If statement
> This is what I currently have
> 
> If SomeCondition = 'SomeArray1'
>   Then Begin
>              SetLength(NewArray, NumberOfElements1 +1);
>              For Counter := 1 to NumberOfElements1 do
>                 Begin
>                   NewArray[Counter] := SomeArray1[Counter];
>                 End;
>            End;
> 
> If SomeCondition = 'SomeArray2'
>   Then Begin
>              SetLength(NewArray, NumberOfElements2 +1);
>              For Counter := 1 to NumberOfElements2 do
>                 Begin
>                   NewArray[Counter] := SomeArray2[Counter];
>                 End;
>            End;
> 
> and so one
> 
> I want to remove every copying parts and replace it like this:
> 
> If SomeCondition = 'SomeArray1'
>   Then Begin
>              NumberOfElements := NumberOfElements1;
>              Pointer to SomeArray1
>            End;
> 
> If SomeCondition = 'SomeArray2'
>   Then Begin
>              NumberOfElements := NumberOfElements2;
>              Pointer to SomeArray2
>            End;
> 
> and than having only one copy part:
> 
> SetLength(NewArray, NumberOfElements +1);
> For Counter := 1 to NumberOfElements do
>   Begin
>      NewArray[Counter] := Get data from Pointer
>   End;
> 
> Is this possible?

You can simply use a helper function

{code: Delphi}

type
  TStringarray : array of string;  //dynamic array type

  function StaticToStringarray( const A: array of string {open
array!}): TStringarray;
  var
    i: integer;
  begin
     SetLength(Result, Length(A));
     for i:= 0 to High(A) do
       Result[i] := A[i];
  end;

You can pass any of your static arrays to this function and get the
equivalent dynamic array back.

  NewArray := StaticToStringarray(SomeArray1);

You have to type NewArray as TStringarray, though.   

-- 
Peter Below (TeamB)  
Don't be a vampire (http://slash7.com/pages/vampires), 
use the newsgroup archives :
http://codenewsfast.com
http://groups.google.com
0
Peter
6/26/2009 5:52:28 PM
Peter Herijgers wrote:

> Hi All,
> 
> I have a lot of arrays like
> 

[snip ...]

> I want to remove every copying parts and replace it like this:
> 
> If SomeCondition = 'SomeArray1'
>   Then Begin
>              NumberOfElements := NumberOfElements1;
>              Pointer to SomeArray1
>            End;
> 
> If SomeCondition = 'SomeArray2'
>   Then Begin
>              NumberOfElements := NumberOfElements2;
>              Pointer to SomeArray2
>            End;
> 
> and than having only one copy part:
> 
> SetLength(NewArray, NumberOfElements +1);
> For Counter := 1 to NumberOfElements do
>   Begin
>      NewArray[Counter] := Get data from Pointer
>   End;
> 
> Is this possible?
> 
> Thanks in advance.
> 
> Peter

If all the arrays have the same type yes.

For the following example create a new VCL project drop a radiogroup and add
the following 5 items
	Some Array 1
	Some Array 2
	Some Array 3
	Some Array 4
	Some Array 5
drop a button and a memo, Copy paste the following code in the implementation
part of the form unit.
--------------------------------------------
// Code to copy paste.
type
  TDynStringArray = array of string;

const
  NumberOfElements1 = 3;
  NumberOfElements2 = 4;
  NumberOfElements3 = 5;
  NumberOfElements4 = 6;
  NumberOfElements5 = 7;

  SomeArray1: Array[1..NumberOfElements1] of String = ('element1', 'element2',
                                                       'element3');
  SomeArray2: Array[1..NumberOfElements2] of String = ('element1', 'element2',
                                                       'element3', 'element4');
  SomeArray3: Array[1..NumberOfElements3] of String = ('element1', 'element2',
                                                       'element3', 'element4',
                                                       'element5');
  SomeArray4: Array[1..NumberOfElements4] of String = ('element1', 'element2',
                                                       'element3', 'element4',
                                                       'element5', 'element6');
  SomeArray5: Array[1..NumberOfElements5] of String = ('element1', 'element2',
                                                       'element3', 'element4',
                                                       'element5', 'element6',
                                                       'element7');

procedure CopyStringArray(var ToArray:TDynStringArray; 
                          const FromArray:array of string);
var
  Cntr: Integer;
begin
  SetLength(ToArray, Length(FromArray)+1);
  for Cntr := Low(FromArray) To High(FromArray) do
  begin
    ToArray[Cntr] := FromArray[Cntr];
  end;
end;
//Code ends
---------------------------------------------------------------


now paste the followin to the buttons on click event.
---------------------------------------------------------------
// On click start
procedure TForm1.Button1Click(Sender: TObject);
Var
  Tmp : TDynStringArray;
  s   : string;
begin
  Memo1.Lines.Clear;
  case RadioGroup1.ItemIndex of
    0: CopyStringArray(Tmp, SomeArray1);
    1: CopyStringArray(Tmp, SomeArray2);
    2: CopyStringArray(Tmp, SomeArray3);
    3: CopyStringArray(Tmp, SomeArray4);
    4: CopyStringArray(Tmp, SomeArray5);
  end;

  for s in Tmp do
  begin
    Memo1.Lines.Add(S);
  end;

end;
//on click end
---------------------------------------------------------------

run the project and press the button.

Regards
Jo.
0
jo
6/26/2009 5:58:29 PM
Reply:

Similar Artilces:

Arrays, Arrays, Arrays
I mainly come from a JScript classic ASP background and I'm liking both C#.net and JScript.net. Now I have to say... "What's happened to arrays?" They used to be so flexible and easy to use. Here are my quick questions... Am I allowed to create an array with a non specified length so that I can add to it? If not then how can I modify an array's length? Can I create an array with a named index eg myArray["apple"] = "tasty"; or is the hash table the equivalent for this now? The classic array methods and properties like pop(), push(), length etc... are ...

referring to an array in an array of arrays
How can I get at an array in an array of arrays? I'm creating the arrays using: $fComments[$i++] = [ @comments ]; Now I want to get back what I put in: print "$fComments[0]\n"; but this prints ARRAY(0x107671f0). How do I follow that pointer? (I really think we need a pre-beginners list... =) TIA. - B You can try something like this: print "@{$fComments[0]}\n"; Which will attempt to dereference the array at $fComments[0]. -----Original Message----- From: Bryan R Harris [mailto:Bryan_R_Harris@raytheon.com] Sent: Tue...

Concatenating arrays from arrays of arrays
Hello - I am trying to accumulate items in a flat list (array) by concatenating on to it the scalars contained in arrays which in turn are contained in arrays. The data is to be read in from a file rather than existing as literals in my code. Everything I try either crashes the computer or gives me an array of arrays instead of a flat list. I have been programming in Unicon which has a concatenate operator, but Perl makes very heavy weather of nested list management. Should I switch to Lisp, or is there a way of doing what I want to do in Perl? Regards _John Sampson_ On...

How to unshift to an array in an array of arrays?
How to unshift to an array in an array of arrays? > -----Original Message----- > From: Angerstein [mailto:ang@nmc-m.dtag.de] > Sent: Wednesday, August 28, 2002 6:12 AM > To: beginners@perl.org > Subject: How to unshift to an array in an array of arrays? > > > How to unshift to an array in an array of arrays? $ perl -d -e 1 ..... DB<1> @aoa = ([qw(foo bar baz)]) << create array of arrays DB<2> x @aoa 0 ARRAY(0x80f82dc) 0 'foo' 1 'bar' 2 'baz' DB<3> unshift @{$aoa[0]}, 'qu...

array of arrays
Hi. =20 Im using a multidimensional array in the following way: $array[$i][$j][$k] =3D [ @list ]; where, @list is a 2-D array ie. = $list[][]. How do I access the individual elements of the array @list once Ive put it = into the array @array? Thanks. -ty On Thu, Oct 18, 2001 at 02:28:41PM -0600, Tyler Cruickshank wrote: > $array[$i][$j][$k] = [ @list ]; where, @list is a 2-D array ie. $list[][]. > > How do I access the individual elements of the array @list once Ive put it > into the array @array? $array[$i][$j][$k][$l][$m] eq $list[$l][$m] Howev...

array of arrays
Hi, I have a simple question: how do I access an array in an array? @array = (@array1, array2); ### unless this is wrong... the idea behind it is, that I have a long list, and want to put all 25 entries in a seperate array in that big array. later on I want to be able to read one specific array at a certain position... -- cheers alex mailto:Keex@gmx.de you need to learn about references. read the: perldoc perllol here is the jist though. @all = (\@array1, \@array2 ... ); # ... so on to access first array and first element...

[@array] and \@array
Given the case, my @array = (1,2,3,4); my $var1 = [@array]; my $var2 = \@array; What's the difference between $var1 and $var2? Are they all refered to @array? Thanks guys. On 3/22/07, Jm lists <practicalperl@gmail.com> wrote: > Given the case, > > my @array = (1,2,3,4); > my $var1 = [@array]; > my $var2 = \@array; > > What's the difference between $var1 and $var2? > Are they all refered to @array? > Thanks guys. $var2 is a reference to @array. $var1 is a reference to new array. Observe #!/usr/bin/perl use strict; use war...

Array of Arrays
Hi, I am looking for a way to create an array of arrays which needs to be passed to Oracle database to minimize the round trips to hte server. I was successful in sending simple arrays so far but no idea on how to create array of arrays in PB. I have used structures to simulate this but looks like I can't send the structure to Oracle. Anyone has any suggestions? Thanks, Anil Please don't cross-post. On 3 Mar 2005 14:02:47 -0800, Anil wrote: >Hi, > >I am looking for a way to create an array of arrays which >needs to be passed to Oracle database t...

Array of arrays
Hi I'm trying to return from a web service a array of arrays, one of the arrays is a array of objects, which I tested separately and it's working fine, and another array of integer and another of strings. When I run the web service I got this:You must implement a default accessor on System.Array because it inherits from ICollectionDefault accessor what that means? Thank you very much.  As Heinlein said "specialization is for insects" explore beyond the universe. Could you pls post the relevant code ? hi rushan thank you for the reply the code is here:  ...

Array of Array.
How is possible to declare a DYNAMIC array of array ? I need something like a dynamic hash table whose pages grow up dynamically too (each one can have different number of elements). In other words, a matrix that can be dynamic in both dimension. example: [1,2,3...] [3,4..] [5] [6,7,8,9...] [.] .. .. .. Have you considered using a DataStore? -- <hopethishelps/> Roy Kiesler [TeamSybase] SDN CodeXchange -- http://codexchange.sybase.com "Marco Meoni - News" <mmeoni@usl4.toscana.it> wrote in message news:401ba036$1@forums-2-dub... > How ...

An array of arrays
Could anybody help me out here? Here's the message I just got from a guy I know: ----------- I was wondering if you know how to create and access an array of arrays in Perl. Basically I'm parsing an exported text file containing e-mails and I think I want to move some fields into arrays until I'm ready to do my report. Here basically is what I'm looking at doing: ^ | | Array 1 (messages) | | v <------ Array 2 -------> (message fields:FROM, TO, CC, etc, sensitivity, etc) I want to be able to search through the first array to count how many messag...

Array of Arrays
Hi, I am looking for a way to create an array of arrays which needs to be passed to Oracle database to minimize the round trips to hte server. I was successful in sending simple arrays so far but no idea on how to create array of arrays in PB. I have used structures to simulate this but looks like I can't send the PB structure to Oracle. Anyone has any suggestions? Thanks, Anil PB 11 or 12 I believe will have this native. <Anil> wrote in message news:422789a4.721c.1681692777@sybase.com... > Hi, > > I am looking for a way to create an array of ar...

Adding array to another array
Hi people :) I have two web methods that are returning two different arrays from web service.Now, is there a possibility that I add those returning arrays to one completely new array? How should I do this? My methods are returning arrays of custom structures.    Break Down the Wall  I'm going to assume Array and not an ArrayList or something like that. Just use the Array.Copy() method.______________________________________Robert Lindley | Sr. Software Engineer - MCP, MCADASTRA Innovations, Ltd. - Better Solutions by Design.---------------------------------...

extracting elements from arrays of arrays of arrays ;-)
Hiya All, I hope you'll forgive this oo perl rookie for asking a routine question. I'm not new to perl, but the bioperl module I'm working on is giving me a headache! ;-) Let's say I have this: my @array = qw(12 56 41 23); my $array_ref = \@array; # where $var1, $var2, and $var3 are other array references. my $ref = [$var1, $var2, $array_ref, $var3 ]; my @object = (); push @object, $ref; QUESTION: How do I obtain the array $array_ref refers to in this situtation? Let's say I want to give @new the values in @array. Thank many thanks for you...

Web resources about - Copy an array to another array by Pointer - embarcadero.delphi.general

Foo Fighters Drop Surprise EP ‘Saint Cecilia’ In Honor Of Paris Attack Victims
The Foo Fighters released a surprise EP entitled Saint Cecilia last night, and it’s free for all fans to stream and download from the band’s ...

Ben Carson Claims He ‘Saw The Film’ of American Muslims Celebrating on 9/11
Republican presidential candidate Ben Carson backed up Donald Trump ‘s claim that thousands of Muslims in New Jersey partied on September 11, ...

Suspect Arrested in New Orleans Med Student Shooting
A Tulane student was shot while helping a woman being attacked, cops say.

Mauricio Macri Asks for Patience After Winning Argentina Election
Wall Street Journal Mauricio Macri Asks for Patience After Winning Argentina Election Wall Street Journal BUENOS AIRES—The conservative mayor ...

Here’s How Much Pfizer Could Save in Taxes After Allergan Merger
The $160 billion mega-merger of pharmaceutical giants Pfizer and Allergan isn’t just about drugs. It’s also about taxes—or rather, avoiding them. ...

Poll Finds American Fearful Of Terrorism, Distrustful Of Obama And Syrian Refugees
A new ABC News/ Washington Post poll shows us that Americans fears of terrorism have ramped up significantly since the terrorist attacks in Paris, ...

The Walking Dead Recap: Use the Stairway, Dummy!
OKAY, I'm happy that I was RIGHT about a certain character surviving, but after the jump, I'm gonna lose my GOT-DAMNED MIND over how it was done! ...

Samsung Offering To Pay Customers Who Sign Up For Its Mobile Payment System
When it comes to competing in the mobile payment arena, Samsung has a plan to convince customers to pay for stuff with its technology: the company ...

Watch: Northern White Rhino's Death at Zoo Leaves Only 3 of Its Kind in the World
Watch: Northern White Rhino's Death at Zoo Leaves Only 3 of Its Kind in the World

Feinstein: Obama’s policies are making ISIS issue worse, not better
Barack Obama has had an increasingly difficult time convincing Americans that his ISIS strategy is working — including the top-ranking member ...

Resources last updated: 11/23/2015 8:11:18 PM