X
PrevPrev Go to previous topic
NextNext Go to next topic
Last Post 30 Jun 2018 05:29 AM by  Ronn Kling
extension of IDL_VALIDNAME
 0 Replies
Sort:
You are not authorized to post a reply.
Author Messages

Ronn Kling



New Member


Posts:5
New Member


--
30 Jun 2018 05:29 AM
    Hello All,

    I found myself needing to be able to identify exactly which character in a string was not valid. IDL_VALIDNAME just returns a null string with no feedback as to where.

    I hope the routine below is useful to everyone!

    Ronn Kling
    www.rlkling.com

    pro test, newTagSet

    if n_params() eq 0 then newTagSet = 'var-72'

    newTagSet = krs_validname(newTagSet, badList)
    if n_elements(badList) ne 0 then begin
    void = dialog_message(/error, ['Tags must not have these characters.', badList.toarray(), 'Try again'])
    endif
    return & end

    ;{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|{{:|

    function krs_validname, inputString, badList
    ;checks to see if all the characters in the inputString are valid and if they are not then identifies which ones are not
    compile_opt idl2

    badList = !NULL
    if strlen(IDL_validname(inputString)) ne 0 then return, inputString
    ;something is wrong, so step through each character to find the bad ones
    numChars = strlen(inputString)
    badList = list()
    for i=0, numChars-1 do begin
    varName = strmid(inputString,i,1)
    ;first character cannot be a number, others can
    if byte(varName) ge 48 and byte(varName) le 57 then begin
    if i eq 0 then badList->add,'First Character Cannot Be A Number'
    endif else if strlen(IDL_validname(strmid(inputString,i,1))) eq 0 then badList->add, varName
    endfor

    return, inputString
    end
    You are not authorized to post a reply.