This topic contains 4 replies, has 3 voices, and was last updated by  Ryan McBee 4 years, 4 months ago.

Hopefully an easy question about Nil checking

  • I’m having trouble handling an XML field that may or may not contanin nil values. How can I check to see if a field contains an nil value and write one if it doesnt? I’m usung the account number in naming a file that iguana creates so I put in an underscore where it doesn’t have one. This if statement works if I have a value, but if I don’t have an account number, then I get an error “Index 1 is out of bounds”

    if patientXML.PatientAccountNumber[1] then
          patientAccount = patientXML.PatientAccountNumber[1]:S()..'_'
          else
          patientAccount = '_'
       end
    end

    Thanks in adavance for any insight you can offer.

    M.R. McBee

    If you change it to this, you’ll probably have more luck:

    if #patientXML.PatientAccountNumber > 0 and patientXML.PatientAccountNumber[1] then
          patientAccount = patientXML.PatientAccountNumber[1]:S()..'_'
          else
          patientAccount = '_'
       end
    end

    That will ensure that patientXML.PatientAccountNumber has a child before drilling into it.

    I have a feeling that this is exactly what you need:

    http://wiki.interfaceware.com/638.html

    Then you can write the code much more elegantly in one line:

    patientAccount = patientXML.PatientAccountNumber:text()..’_’

    Where the text() function is a general reusable ‘node extension’ function.

    The source to the text() function is in the above wiki page. You put that into a general re-usable module that you use anytime you are dealing with XML.

    It make the code more readable and easier to understand what it’s doing. Hope that helps!

    Thanks guys, I did a few searches through the wiki and didn’t find that one. But that’s a perfect solution, I appreaciate your help. (Kevins)

    M.R. McBee

Tagged: 

You must be logged in to reply to this topic.