Reply To: Mod 11 check digit

Clovertech Forums Read Only Archives Cloverleaf Cloverleaf Mod 11 check digit Reply To: Mod 11 check digit

#59277
Vince Angulo
Participant

    Haven’t seen Modulus 11 since my AS/400 days.  It’s easier to work if you reverse the input value.  This should work:

    Code:


    # create a list from the input value
    set mInput [split $xlateInVals {}]

    # reverse the list values to a new list
    set mLength [llength $mInput]
    set a [expr $mLength – 1]
    set mReverse {}
    foreach i $mInput {
    lappend mReverse [lindex $mInput $a]
    incr a -1
    }

    # create a list with enough repeating weight elements
    # there are different weight values, AS/400 commonly used 2-7
    set mCount [expr int($mLength / 6) + 1]
    set mWeight {}
    for {set j 0} {$j < $mCount} {incr j} {
    lappend mWeight 2 3 4 5 6 7
    }

    # aggregate the products of the input and weight elements
    set mSum 0
    for {set k 0} {$k < $mLength} {incr k} {
    set mSum [expr $mSum + ([lindex $mReverse $k] * [lindex $mWeight $k])]
    }

    # determine and evaluate the remainder
    set mMod [expr $mSum % 11]
    switch — $mMod {
    0; {set mMod11 "0"}
    1; {set mMod11 "X"}
    default {set mMod11 [expr 11 – $mMod]}
    }

    # tag the check digit onto the input list and turn it back to a string
    set xlateOutVals [join [lappend mInput $mMod11] {}]

    Not exactly elegant, but functional…Maybe someone with stronger tcl chops can simplify it!!