Menu

#2616 Wrong array size for empty arrays

None
closed-fixed
nobody
2023-05-19
2023-04-19
theozh
No

gnuplot returns the wrong array size for empty arrays. Although for datablocks it returns the correct number, but not for arrays.

Example:

$Data <<EOD
1
2
3
EOD
print |$Data|

$Data <<EOD
EOD
print |$Data|

array A = [1]
print |A|

array A = [1,2,3]
print |A|

array A = []
print |A|

Returns where the last 1 is not correct. Tested with gnuplot 5.4.6

3
0
1
3
1    # should be 0

Discussion

  • theozh

    theozh - 2023-04-19
    • labels: --> array, size, cardinality
    • Group: -->
    • Priority: -->
     
  • Ethan Merritt

    Ethan Merritt - 2023-04-19

    True. But I'm curious. Did you find a way to end up with an empty array in version 5.4, other than simply creating it empty as in your test case?

    The code in version 6 gets this right, and there it actually matters. When arrays became full-fledged variables in development version 5.5, empty arrays started to be a reasonable thing. You could operate on an array or create and return one from a function; in either case it might end up empty. For instance you could start with an empty array and add elements to it. I don't think there's any way to do that in version 5.4 so the question of how big the array currently is doesn't come up in practice. But maybe I'm missing something clever, which is why I ask if you found this via something more complicated that an empty declaration.

    Anyhow, now fixed for 5.4.7

     
  • Ethan Merritt

    Ethan Merritt - 2023-04-19
    • status: open --> pending-fixed
     
  • theozh

    theozh - 2023-04-20

    Thank you for the quick response.
    You are right, there is probably no way in gnuplot 5.4 to create an empty array other than defining an empty one.

    Since arrays are convenient in many cases, I was thinking about this when having a gnuplot template script and a programming language modifying it, e.g. with the output of a calculation, which is a sequence of numbers, but which could also be no number.

    A simplified example:

    array A = [] # this line will be replaced by external script, but array could also stay empty
    
    plot for [i=1:|A|] sin(A[i]*x) w l ti sprintf("%g",A[i])   
    

    If A is empty, instead of plotting nothing the script would fail because |A|=1 instead of =0 : uninitialized user variable.

     
  • Ethan Merritt

    Ethan Merritt - 2023-05-19
    • Status: pending-fixed --> closed-fixed
     

Log in to post a comment.