QB64 Phoenix Edition
Swapping array elements - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10)
+---- Thread: Swapping array elements (/showthread.php?tid=1142)

Pages: 1 2 3


RE: Swapping array elements - Pete - 11-18-2022

But my FUNCTON is a one-liner...

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

PRINT goswap$(a$, 3, 5)

FUNCTION goswap$ (a$, x, y)
    goswap$ = MID$(a$, 1, x - 1) + MID$(a$, y, 1) + MID$(a$, x + 1, y - (x + 1)) + MID$(a$, x, 1) + MID$(a$, y + 1)
END FUNCTION

Pete Tongue Tongue Tongue Tongue Tongue Tongue Tongue Tongue Tongue Tongue Tongue Tongue Tongue Tongue

PS Love the new perks in the Quick Reply. Good job!!!


RE: Swapping array elements - JRace - 11-18-2022

Alright, dadburn it, you guys made me think!

In-string swap with no temps and no other changes to string:

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

Print a$

Mid$(a$, 3, 1) = Chr$(Asc(Mid$(a$, 3, 1)) Xor Asc(Mid$(a$, 5, 1)))
Mid$(a$, 5, 1) = Chr$(Asc(Mid$(a$, 3, 1)) Xor Asc(Mid$(a$, 5, 1)))
Mid$(a$, 3, 1) = Chr$(Asc(Mid$(a$, 3, 1)) Xor Asc(Mid$(a$, 5, 1)))

Print a$

(https://en.wikipedia.org/wiki/XOR_swap_algorithm)


RE: Swapping array elements - Pete - 11-18-2022

[Image: ?u=https%3A%2F%2Fi.gifer.com%2Fembedded%...ipo=images]


RE: Swapping array elements - PhilOfPerth - 11-18-2022

Thanks all, but simplicity is my middle name, and I still think the method I proposed in post #6 is the simplest, even though it uses 2 new variables.
Interesting though that none of the suggestions used the SWAP function. Seems it has limited use, at least where string elements are concerned. Undecided


RE: Swapping array elements - Pete - 11-18-2022

The last string operation I used SWAP on was string math routines where comparisons were being made and in some instances the larger of the two needed to be the first variable to be passed to the function, hence: IF  LEN(a$) > LEN(b$) THEN SWAP a$,  b$

Pete


RE: Swapping array elements - SMcNeill - 11-18-2022

SWAP is great for use in sort routines.


RE: Swapping array elements - bplus - 11-18-2022

Here is LetterSwap$ Function that actually uses Swap!
Code: (Select All)
a$ = "ABCDEF"
Print "3, 5 "; LetterSwap$(a$, 3, 5)
Print "1, 4 "; LetterSwap$(a$, 1, 4)
Print "4, 2 "; LetterSwap$(a$, 4, 2)

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

Function LetterSwap$ (a$, n, m)
    Dim L$(1 To Len(a$))
    For i = 1 To Len(a$)
        L$(i) = Mid$(a$, i, 1)
    Next
    Swap L$(n), L$(m)
    rtn$ = Space$(Len(a$))
    For i = 1 To Len(a$)
        Mid$(rtn$, i, 1) = L$(i)
    Next
    LetterSwap$ = rtn$
End Function

Swap also great for scrambling and shuffling!

And once again, @PhilOfPerth Swap needs 2 variables to switch their values. Letter swapping in a string simply involves one variable.

Neither could you: swap "word1", "word2"


RE: Swapping array elements - mnrvovrfc - 11-18-2022

Character swap could be done easily in C. Enough said. Otherwise we would have to go into the realm of object-oriented programming. I'm not sure now but Delphi/Free Pascal had a class that allowed an ordinary string to be handled according to its constituent characters, allowed square-bracket operator like "word[1]" instead of like in BASIC "MID$(word$, 2, 1)". This was to access the second character of "word" string variable. Saying "word(1)" instead of "word[1]" probably called the class constructor. What was confusing is that array elements always started at zero, and the bad design decision was taken not to allow low and high array bound definition like a normal Pascal array.


RE: Swapping array elements - mnrvovrfc - 11-18-2022

(11-18-2022, 12:36 AM)SMcNeill Wrote: You might want to swap to ASC over MID$, due to the speed and performance difference between the two commands.
"ASC" might make a good keyword of the day, "my gosh how it's grown" or "not if you went to Dartmouth" or something like that.


RE: Swapping array elements - bplus - 11-18-2022

(11-18-2022, 02:49 PM)mnrvovrfc Wrote:
(11-18-2022, 12:36 AM)SMcNeill Wrote: You might want to swap to ASC over MID$, due to the speed and performance difference between the two commands.
"ASC" might make a good keyword of the day, "my gosh how it's grown" or "not if you went to Dartmouth" or something like that.

Yes, it is very interesting that it can be used like the MID$ Statement!!!
Referring to this:
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

You can alter the ASC's and not bother with CHR$() to convert back to string.

Also I agree with PhilOfPerth that using his 2 variable change is nice simple straight forward solution too.