UPDATE :: a demo of what the effects are for the 3 types of swapping methods available in awk
— (awk
doesn't offer true bit-wise XOR
) :
either substr() wrapper
or function and/or array
are truly lossless for any input, even when swapping arbitrary binary data in gawk unicode-mode
,
or when swapping completely different data types, like a
unicode string with an approximation of euler's e
that's beyond
IEEE 754
double-prec floating point precision
arithmetic-based swaps
are only lossless when both inputs are actually numeric, not potentially subject to special interpretation rules, e.g. built-in hex decoding in some awk
variants when you place a unary +
or -
in front of it, AND both inputs are entirely within the current precision level. USE IT WITH CAUTION
echo '91223951291111 '\
'8777175273333\n' \
\
'abc@\uF8FF_123^456#\0306\02222\\|/<>[]{\0354\0210\0657}()&+- '\
'2.7182818284590452353602874713526624977
5724709369995957496696762772407663035
35475945713821785251\n' \
\
'FTX_ThreeArrows_Luna_Terra ' \
'abc345\0301\0375fgc' |
LANG='en_US.UTF-8' LC_ALL= gawk -e '
BEGIN {
1 __________ = "\f\r\t\t..."
} {
3 split(_=__=___ = "",____)
}
{
3 print ">ORIGINAL_INPUT :: ", _ = $++__,
__________, __ = $++__,
"\n--------------- :: --------------"\
"--------------\n"
3 _ = __ substr("", (__=_)^(_<_))
3 print "substr(..)-swap :: ", _, __________, __
3 ____[++___] = _
3 ____[++___] = __
3 swap(____)
3 __ = ____[___--]
3 _ = ____[___--]
3 print "func+array-swap :: ",_,__________,__
3 _ -= __ = (_+=__) - __
3 print "arithmetic-swap :: ",_,__________,__
3 print "__________"
}
function swap(__,_,___) {
___ = __[++_]
__[_] = __[_+_]
__[_+_] = ___
}'
>ORIGINAL_INPUT :: 91223951291111
... 8777175273333
--------------- :: -------------- --------------
substr(..)-swap :: 8777175273333
... 91223951291111
func+array-swap :: 91223951291111
... 8777175273333
arithmetic-swap :: 8777175273333
... 91223951291111
__________
>ORIGINAL_INPUT :: abc@_123^456#ƒ2\|/<>[]{숯}()&+-
... 2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251
--------------- :: -------------- --------------
substr(..)-swap :: 2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251
... abc@_123^456#ƒ2\|/<>[]{숯}()&+-
func+array-swap :: abc@_123^456#ƒ2\|/<>[]{숯}()&+-
... 2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251
arithmetic-swap :: 2.71828
... 0
__________
>ORIGINAL_INPUT :: FTX_ThreeArrows_Luna_Terra
... abc345??fgc
--------------- :: -------------- --------------
substr(..)-swap :: abc345??fgc
... FTX_ThreeArrows_Luna_Terra
func+array-swap :: FTX_ThreeArrows_Luna_Terra
... abc345??fgc
arithmetic-swap :: 0
... 0
__________
In awk
, without needing a temp variable, a temp array, an external function call, substring-ing their values out of one another, XOR operations, or even any math operations of any sort,
this trick works for whether their values are numeric, string, or even arbitrary bytes that aren't UTF8-compliant, nor do the types of the 2 variables even need to match :
gawk/mawk/nawk '{
a = (b) \
substr("", ( b = (a) )^"")
}'
Even in Unicode-locale, gawk
unicode mode could swap the values of arbitrary non-UTF8 bytes
without any error messages.
No need to use C or POSIX locale.
Why it works is that in the process of this concatenation, the original value of b
has already been placed in some system-internal staging ground, so the sub-sequent assignment of b's value into a
does not have any impact into what's being assigned into a
The second half of it is taking a substring of an empty string, so of course nothing comes out, and doesn't affect the first half.
After b = a
, I immediately take the zero-th power of it, which always returns a 1 in awk
so the latter portion becomes
# in awk, string indices start from 1 not 0
substr(EMPTY_STRING, STARTING-POSITION-OF-ONE)
of course nothing comes out of it, which is the desired effect, so the clean original value of b
could be assigned into a
.
This isn't taking a substring of a
's or b
's value. It's taking advantage of dynamic typing properties of awk.
A XOR-based approach, which clean, still require 3 operations, plus a safety check against being memory position (for C). And for gnu-awk, its xor()
function only works with non-negative numeric inputs.
This one-liner approach circumvents all the common issues with value swapping.
Either a
or b
, or even both, being an array cell, regardless of single or multi-dimensional, e.g.
arr[ _pos_idx_a_ ]
works exactly the same with this one-liner approach. The only limitation I could think of is that this approach cannot directly swap full contents of an entire array with another array.
I thought of adding a check for a==b to avoid a double assignment, but then realized the internal system resources needed to perform such a check is not worth the minuscule amount of time saved.