Swapping array elements - Printable Version +- QB64 Phoenix Edition (https://staging.qb64phoenix.com) +-- Forum: QB64 Rising (https://staging.qb64phoenix.com/forumdisplay.php?fid=1) +--- Forum: Code and Stuff (https://staging.qb64phoenix.com/forumdisplay.php?fid=3) +---- Forum: Help Me! (https://staging.qb64phoenix.com/forumdisplay.php?fid=10) +---- Thread: Swapping array elements (/showthread.php?tid=1142) |
RE: Swapping array elements - Pete - 11-18-2022 But my FUNCTON is a one-liner... Code: (Select All) a$ = "ABCDEFG" Pete 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" (https://en.wikipedia.org/wiki/XOR_swap_algorithm) RE: Swapping array elements - Pete - 11-18-2022 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. 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" 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" 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. |