I reproduced this in
GNU Awk 4.2.0, API: 2.0
However, I also "fixed" it by forcing more *local* vars: (you should be
able to delete my extra debug printing)
function str_num_to_a(i, a*, x*) {
print ">", i, typeof(i)
print match(i, /"([^0-9]*)([0-9]+)"/, a)
for (x in a) {
print ">>", a[x], typeof(a[x])
}
}
function str_num_cmp(i1, v1, i2, v2*, a1, a2*) {
str_num_to_a(i1, a1)
str_num_to_a(i2, a2)
if(a1[1] < a2[1])
return -1
if(a1[1] == a2[1]) {
if(a1[2] < a2[2])
return -1
if(a1[2] == a2[2])
return 0
}
return 1
}
BEGIN {
keys["foo6"]
keys["foo10"]
keys["\"foo6\""]
keys["\"foo10\""]
PROCINFO["sorted_in"] = "str_num_cmp"
for (k in keys) {
print k, keys[k]
}
}
On Fri, Feb 25, 2022 at 7:37 AM Ed Morton<mortoneccc@comcast.net> wrote:
Running on cygwin with:
$ $SHELL --version
GNU bash, version 4.4.12(3)-release (x86_64-unknown-cygwin)
$ awk --version
GNU Awk 5.1.1, API: 3.1 (GNU MPFR 4.1.0, GNU MP 6.2.1)
I was reviewing someone else's code that has a user-provided comparison
function to sort alphanumeric strings like "foo123" alphabetically by
the alphabetic part and numerically by the numeric part so that "foo6"
would come before "foo10". I wasn't sure if the results of the match()
in their first function were strings or strnums so I added a loop in the
first function to dump each value in a[] and it's type after the match()::
$ cat tst.awk
function str_num_to_a(i, a) {
match(i, /"([^0-9]*)([0-9]+)"/, a)
for (x in a) {
print a[x], typeof(a[x])
}
}
function str_num_cmp(i1, v1, i2, v2) {
str_num_to_a(i1, a1)
str_num_to_a(i2, a2)
if(a1[1] < a2[1])
return -1
if(a1[1] == a2[1]) {
if(a1[2] < a2[2])
return -1
if(a1[2] == a2[2])
return 0
}
return 1
}
BEGIN {
keys["foo6"]
keys["foo10"]
#keys["\"foo6\""]
#keys["\"foo10\""]
PROCINFO["sorted_in"] = "str_num_cmp"
for (k in keys) {
print k, keys[k]
}
}
The above code works fine BUT then try uncommenting the 2 `#keys` lines
in the BEGIN section (doesn't matter if you comment out the other 2 keys
lines or not) and you'll get:
$ awk -f tst.awk
awk: tst.awk:17: fatal: internal error line 1349, file:
/home/corinna/tmp/gawk-5.1.1/gawk-5.1.1-1.x86_64/src/gawk-5.1.1/eval.c
Line 17 is the "return 0" in the middle function. Now comment out the
loop in the top function and the code will work again. I tried to reduce
this example further (it's already reduced from the original) but after
a lot of trial and error I surrendered.
Ed.