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) |
Swapping array elements - PhilOfPerth - 11-18-2022 I'm working on a prog that needs to sort a string, by swapping elements of the string. Code: (Select All) Screen 9 RE: Swapping array elements - bplus - 11-18-2022 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$ RE: Swapping array elements - SMcNeill - 11-18-2022 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! I think what you're looking for is something more like: Code: (Select All) a$ = "ABCDEFG" RE: Swapping array elements - bplus - 11-18-2022 Code: (Select All) a$ = "12345" RE: Swapping array elements - SMcNeill - 11-18-2022 (11-18-2022, 12:27 AM)bplus Wrote: 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. RE: Swapping array elements - PhilOfPerth - 11-18-2022 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" RE: Swapping array elements - TerryRitchie - 11-18-2022 (11-18-2022, 12:46 AM)PhilOfPerth Wrote: Thanks for that! 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) RE: Swapping array elements - JRace - 11-18-2022 @PhilOfPerth : Quote:But if I don't use the SWAP function, I can just do this: Actually, you only need to introduce one new temp var: Code: (Select All) t1$ = Mid$(a$, 3, 1) 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 (Wow, this post was brand new with no responses when I pulled it up. That's what I get for taking a break.) RE: Swapping array elements - Pete - 11-18-2022 TEMP VARIABLES? WE DON'T NEED NO STINKIN' TEMP VARIABLES! Code: (Select All) a$ = "ABCDEFG" 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 RE: Swapping array elements - SMcNeill - 11-18-2022 Swapping letter positions inside a string, *without* the use of a single temp variable. Code: (Select All) a$ = "ABCDEFG" |