Swapping array elements
#1
I'm working on a prog that needs to sort a string, by swapping elements of the string. 
Code: (Select All)
Screen 9
_FullScreen

a$ = "ABCDEFG"
Print Mid$(a$, 3, 1), Mid$(a$, 5, 1)
swap mid$(a$,3,1),mid$(a$,5,1)
?a$
From the description of SWAP in Help, I can do this by naming the elements, but I can't get this to work! In the small sample, the SWAP line is illegal. What am I doing wrong? Obviously, that line was not accepted, as it didn't get capitalized.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Reply
#2
For swap you switch two variable values.

Swapping letters in a word, interesting.

t$ = mid$(a$, 5, 1)
mid$(a$, 5, 1) = mid$(a$, 3, 1)
mid$(a$, 3, 1) = t$
b = b + ...
Reply
#3
You're not swapping any sort of variables...

a$ = "ABCDEFG"
swap mid$(a$,3,1),mid$(a$,5,1)

Evaluate those mid$ functions down:

SWAP "C", "E"

You're not swapping any variable values like that!  Wink


I think what you're looking for is something more like:

Code: (Select All)
a$ = "ABCDEFG"

Print SwapString(a$, 3, 5)

Function SwapString$ (s_passed$, pos1, pos2)
    s$ = s_passed$ 'so we don't change by parameter
    temp = Asc(s$, pos1)
    Asc(s$, pos1) = Asc(s$, pos2)
    Asc(s$, pos2) = temp
    SwapString$ = s$
End Function
Reply
#4
Code: (Select All)
a$ = "12345"
swapLetters a$, 3, 5
Print a$
swapLetters a$, 1, 4
Print a$

Sub swapLetters (a$, n, m)
    t$ = Mid$(a$, n, 1)
    Mid$(a$, n, 1) = Mid$(a$, m, 1)
    Mid$(a$, m, 1) = t$
End Sub
b = b + ...
Reply
#5
(11-18-2022, 12:27 AM)bplus Wrote:
Code: (Select All)
a$ = "12345"
swapLetters a$, 3, 5
Print a$
swapLetters a$, 1, 4
Print a$

Sub swapLetters (a$, n, m)
    t$ = Mid$(a$, n, 1)
    Mid$(a$, n, 1) = Mid$(a$, m, 1)
    Mid$(a$, m, 1) = t$
End Sub

You might want to swap to ASC over MID$, due to the speed and performance difference between the two commands.  Letter swapping is usually something which ends up getting called inside loops, and it could really make a difference in how quickly the overall program performs here.  Wink
Reply
#6
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.
Of all the places on Earth, and all the planets in the Universe, I'd rather live here (Perth, W.A.) Big Grin
Reply
#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
#8
@PhilOfPerth

Quote: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.


Actually, you only need to introduce one new temp var:
Code: (Select All)
t1$ = Mid$(a$, 3, 1)
Mid$(a$, 3, 1) = Mid$(a$, 5, 1)
Mid$(a$, 5, 1) = t1$

Off the top of my head I don't see a way to swap characters within a single string without some old-fashioned Varptr/Peek/Poke shenanigans, and I don't know how easy that would be to pull off in QB64.


Anyway, here's a slight mod to your original code snippet:
Code: (Select All)
Screen 9
_FullScreen

a$ = "ABCDEFG"
Print Mid$(a$, 3, 1), Mid$(a$, 5, 1)

'The fourth parameter allows you to swap more than one character at a time:
myswap a$, 3, 5, 1 ' swap mid$(a$,3,1),mid$(a$,5,1)
'Below is the swapping code for inline use, if desired.
't$ = Mid$(a$, 3, 1): Mid$(a$, 3, 1) = Mid$(a$, 5, 1): Mid$(a$, 5, 1) = t$

Print a$


Sub myswap (s$, e1, e2, l)
    t$ = Mid$(s$, e1, l)
    Mid$(s$, e1, l) = Mid$(s$, e2, l)
    Mid$(s$, e2, l) = t$
End Sub


(Wow, this post was brand new with no responses when I pulled it up.  That's what I get for taking a break.)
Reply
#9
TEMP VARIABLES? WE DON'T NEED NO STINKIN' TEMP VARIABLES!

Code: (Select All)
a$ = "ABCDEFG"
PRINT a$
a$ = MID$(a$, 1, 3 - 1) + MID$(a$, 5, 1) + MID$(a$, 3 + 1, 5 - (3 + 1)) + MID$(a$, 3, 1) + MID$(a$, 5 + 1)
PRINT a$
SLEEP
PRINT

a$ = "ABCDEFG"
INPUT "Swap letter #1-7: "; x
INPUT "With letter #1-7: "; y
a$ = MID$(a$, 1, x - 1) + MID$(a$, y, 1) + MID$(a$, x + 1, y - (x + 1)) + MID$(a$, x, 1) + MID$(a$, y + 1)
PRINT a$

Kidding aside, I generally do things as J described. Very easy to understand and only requires one temp variable, which I usually name as tmp$.

Anyway, the obfuscated function I posted was at least worth a laugh to post.

Pete
Reply
#10
Swapping letter positions inside a string, *without* the use of a single temp variable.

Code: (Select All)
a$ = "ABCDEFG"

SwapLetter a$, 3, 5
Print a$

Sub SwapLetter (a$, p1, p2)
    a$ = a$ + Mid$(a$, p1, 1)
    Mid$(a$, p1) = Mid$(a$, p2, 1)
    Mid$(a$, p2) = Right$(a$, 1)
    a$ = Left$(a$, Len(a$) - 1)
End Sub
Reply




Users browsing this thread: 4 Guest(s)