Swapping array elements
#7
(11-18-2022, 12:46 AM)PhilOfPerth Wrote: Thanks for that!
You pointed me in the right direction, but I still don't really understand why I can't just swap the two elements. 
But if I don't use the SWAP function, I can just do this:
Code: (Select All)
a$ = "ABCDEFG"
Print a$
t1$ = Mid$(a$, 3, 1): t2$ = Mid$(a$, 5, 1)
Mid$(a$, 3, 1) = t2$: Mid$(a$, 5, 1) = t1$
Print a$
It introduces two new variables, but that's ok in my case. I'll look at using ASC instead of mid$ too, though speed is not important in my case either.

The numbers contained in MID$() are not elements, they are string positions. Elements are contained within arrays:

DIM A(5, 10, 20)

5, 10, and 20 are your elements, often referred to as indexes.

MID$(a$, 10, 20)

a$ is the string to work with
10 is the start position within that string
20 is the number of characters, counting the start position, to extract from the string.

SWAP only works with variables, not positions within variables.

DIM a(10, 10) AS INTEGER
DIM b(10, 10) AS INTEGER

You can swap an entire array of variables (as long as the arrays are of equal size and type):

SWAP a, b

or individual elements within the array (which are simply variable values):

SWAP a(1, 3), b(3, 7)
Reply


Messages In This Thread
Swapping array elements - by PhilOfPerth - 11-18-2022, 12:06 AM
RE: Swapping array elements - by bplus - 11-18-2022, 12:22 AM
RE: Swapping array elements - by SMcNeill - 11-18-2022, 12:23 AM
RE: Swapping array elements - by bplus - 11-18-2022, 12:27 AM
RE: Swapping array elements - by SMcNeill - 11-18-2022, 12:36 AM
RE: Swapping array elements - by mnrvovrfc - 11-18-2022, 02:49 PM
RE: Swapping array elements - by JRace - 11-18-2022, 04:19 PM
RE: Swapping array elements - by PhilOfPerth - 11-18-2022, 12:46 AM
RE: Swapping array elements - by TerryRitchie - 11-18-2022, 01:09 AM
RE: Swapping array elements - by JRace - 11-18-2022, 01:40 AM
RE: Swapping array elements - by Pete - 11-18-2022, 02:24 AM
RE: Swapping array elements - by SMcNeill - 11-18-2022, 02:41 AM
RE: Swapping array elements - by Pete - 11-18-2022, 02:51 AM
RE: Swapping array elements - by JRace - 11-18-2022, 03:42 AM
RE: Swapping array elements - by Pete - 11-18-2022, 04:15 AM
RE: Swapping array elements - by PhilOfPerth - 11-18-2022, 05:45 AM
RE: Swapping array elements - by Pete - 11-18-2022, 05:57 AM
RE: Swapping array elements - by SMcNeill - 11-18-2022, 12:04 PM
RE: Swapping array elements - by bplus - 11-18-2022, 01:02 PM
RE: Swapping array elements - by mnrvovrfc - 11-18-2022, 02:40 PM
RE: Swapping array elements - by bplus - 11-18-2022, 03:08 PM
RE: Swapping array elements - by bplus - 11-18-2022, 04:21 PM
RE: Swapping array elements - by Pete - 11-18-2022, 06:54 PM
RE: Swapping array elements - by PhilOfPerth - 11-19-2022, 12:12 AM
RE: Swapping array elements - by Pete - 11-19-2022, 12:16 AM



Users browsing this thread: 1 Guest(s)