[AccessD] Swap two elements in an array

James Button jamesbutton at blueyonder.co.uk
Fri Aug 19 12:16:44 CDT 2022


It may be a somewhat low process for items in a large array, depending on the
need, or not for the array handling to create a new version of the array and
copy everything from the old instance.

I'll leave you do check if that applies to the array you have.

If that does cause slow processing, maybe have an additional array with the
replacement values, and in that including a key value indicating which element
of the large base array the new entry is to apply for.

Also - if anyone has a definite knowledge of the current memory management
associated with arrays, could they post a summary to update my memory!

JimB.

  

-----Original Message-----
From: AccessD
<accessd-bounces+jamesbutton=blueyonder.co.uk at databaseadvisors.com> On Behalf Of
Arthur Fuller
Sent: Thursday, August 18, 2022 5:17 PM
To: Access Developers discussion and problem solving
<accessd at databaseadvisors.com>
Subject: Re: [AccessD] Swap two elements in an array

Thanks, Stuart.

After sending my message, I rolled my own, using exactly the same technique
as you -- couldn't think of a way around the temp variable.

On Thu, Aug 18, 2022 at 9:41 AM Stuart McLachlan <stuart at lexacorp.com.pg>
wrote:

> Since VBA doesn't have a SWAP function, you have to do it with a temporary
> variable
> Assuming it's a one dimensional array, something like:
>
> Temp = Arr(2) : Arr(2) = Arr(4) : Arr(4) = Temp
> If you define Temp as a Variant, you can use it with any type of array.
>
> If it's a multi-dimensional array, you have to sway each dimension:
> Temp = Arr(1,2) : Arr(1,2) = Arr(1,4) : Arr(1,4) = Temp
> Temp = Arr(2,2) : Arr(2,2) = Arr(2,4) : Arr(2,4) = Temp
> Temp = Arr(3,2) : Arr(3,2) = Arr(3,4) : Arr(3,4) = Temp
> ...
>
>
> On 18 Aug 2022 at 8:35, Arthur Fuller wrote:
>
> > What is the best way to swap two elements in an array? Typically they
> > are adjacent but ideally I want a method that can swap any two
> > elements.
> >
> > --
> > Arthur
> > --
> > AccessD mailing list
> > AccessD at databaseadvisors.com
> > https://databaseadvisors.com/mailman/listinfo/accessd
> > Website: http://www.databaseadvisors.com
> >
>
>
> --
> AccessD mailing list
> AccessD at databaseadvisors.com
> https://databaseadvisors.com/mailman/listinfo/accessd
> Website: http://www.databaseadvisors.com
>


-- 
Arthur
-- 
AccessD mailing list
AccessD at databaseadvisors.com
https://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com



More information about the AccessD mailing list