Menu

#284 Test if array element is defined

None
closed
nobody
None
5
2024-09-26
2023-05-14
No

Hey,
I'm wondering if there is a way to test if an array element is defined?
My current work around works but is not very handy:

array x[5]=[1,2]
t=x[3]
if(exists("t")) pr "Element defined"

Thanks,
Alex

Discussion

  • Ethan Merritt

    Ethan Merritt - 2023-05-17

    I think that is about the only way in version 5.
    In version 6 you will be able to use a function block to reduce that to a single test:

    function $Exists(foo) << EOF
        local B = 1
        eval( "B = ".foo )
        return exists("B")
    EOF
    
    array A[3]
    A[2] = "two"
    
    do for [x in "A[1] A[2] A[3]"] {
        if ($Exists(x)) { print x, " exists" }
        else { print x, " does not exist" }
    }
    

    [~/temp] gnuplot E.gp
    A[1] does not exist
    A[2] exists
    A[3] does not exist

     
  • Ethan Merritt

    Ethan Merritt - 2024-09-26
    • status: open --> closed
    • Group: -->
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.