A strange behavior with the use of arrays and stats was observed here:
https://stackoverflow.com/q/78111614/7295599
I assume this holds for all gnuplot versions >=4.6.0
Minimal example:
### strange behaviour with setting arrays and using stats
reset session
$Data <<EOD
1 10
2 20
3 30
4 40
EOD
stats $Data u (c=$0+1) nooutput # get the number of rows into c
array M[c]
array M_x_N[c]
stats $Data u (M[int($0+1)] = $1) name "M" nooutput
stats $Data u (M_x_N[int($0+1)] = $2) name "M_x_N" nooutput
### end of script
This will fail with an error: ')' expected.
However, if you change the sequence to the following, it will work.
array M[c]
stats $Data u (M[int($0+1)] = $1) name "M" nooutput
array M_x_N[c]
stats $Data u (M_x_N[int($0+1)] = $2) name "M_x_N" nooutput
Another version with the original sequence, i.e. all array definitions first and then all stats commands will work as expected, if you change the name of the array from M_x_N to MxN.
array M[c]
array MxN[c]
stats $Data u (M[int($0+1)] = $1) name "M" nooutput
stats $Data u (MxN[int($0+1)] = $2) name "MxN" nooutput
So, a simple workaround when using stats would be: avoid underscores in array names (or don't use identically starting array names together with underscores)
An explanation what's going on in detail is given by Ethan here:
https://stackoverflow.com/a/78117797/7295599
If it can't be fixed easily, a mention in the documentation would be helpful.
After looking at the code, I blame myself for committing an incomplete fix about five years ago (83a349a0). Deletion of an explicit list of variable names was added, but the original "delete everything" line elsewhere in the code was not removed.
Fixed now.
Thank you for the quick fix!