Announcing the new Datavyu v1.3.6 with enhanced features and transcription support!

Greetings,

We'll be utilizing the checkValidCodes function for validating most of the codes in our columns.

I'm wondering if it's also possible to do pattern-based validation using regular expressions? For example, suppose I'd like to ensure that every value in a particular column/code ends with a particular punctuation mark (e.g., /\.\!\?$/).

If you have any example ruby scripts that we could use as a template for this type of validation, we'd be most grateful!

asked 31 Aug '15, 13:47

jvoigt's gravatar image

jvoigt
1224
accept rate: 0%


This is a feature that should be available soon! For now, you can place the following into individual scripts:


def checkValidCodes2(var, dump_file, *arg_code_pairs)
    if var.class == "".class
        var = getVariable(var)
    end

    if dump_file != ""
        if dump_file.class == "".class
            dump_file = open(dump_file, 'a')
        end
    end

    # Make the argument/code hash
    arg_code = Hash.new
    for i in 0...arg_code_pairs.length
      if i % 2 == 0
        if arg_code_pairs[i].class != "".class
            print_debug 'FATAL ERROR in argument/valid code array.  Exiting.  Please check to make sure it is in the format "argumentname", ["valid","codes"]'
            exit
        end

        arg = arg_code_pairs[i]
        if ["0","1","2","3","4","5","6","7","8","9"].include?(arg[1].chr)
            arg = arg[1..arg.length]
        end
        arg = arg.gsub(/(\W )+/,"").downcase

        # Add the filter for this code.  If the given filter is an array, convert it to a regular expression using Regex.union
        filter = arg_code_pairs[i+1]
        if(filter.class == Array)
            arg_code[arg] = Regexp.new('\A(' + Regexp.union(filter).source + ')\Z')
        elsif(filter.class == Regexp)
            arg_code[arg] = arg_code_pairs[i+1]
        else
            print_debug "FATAL ERROR in argument/valid code array: expected array or regular expression for filtering #{arg}, received #{filter.class}."
            raise "Unhandled filter type: #{filter.class}"
        end
      end
    end

    errors = false
    for cell in var.cells
        for arg, code in arg_code
        val = eval "cell.#{arg}"
        if code.match(val).nil?
            errors = true
            str = "Code ERROR: Var: " + var.name + "\tOrdinal: " + cell.ordinal.to_s + "\tArg: " + arg + "\tVal: " + val + "\n"
            print str
            if dump_file != ""
                dump_file.write(str)
            end
        end
        end
    end
    if not errors
    print_debug "No errors found."
    end
end

You can then run this like the regular checkValidCodes() but with regular expressions as well as arrays for valid codes.

permanent link

answered 31 Aug '15, 14:08

Shohan%20Hasan's gravatar image

Shohan Hasan ♦♦
381126
accept rate: 11%

This looks promising. Thanks!

(31 Aug '15, 14:36) jvoigt jvoigt's gravatar image

Hmm. This looks promising. Thanks. / jv

permanent link

answered 31 Aug '15, 14:36

jvoigt's gravatar image

jvoigt
1224
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×46
×18
×1
×1

question asked: 31 Aug '15, 13:47

question was seen: 3,422 times

last updated: 31 Aug '15, 14:36

powered by OSQA