2017-02-23 26 views
0

我不會寫Ruby代碼,但我發現這個Ruby代碼來計算校驗和AEMO NMIAEMO校驗支持Ruby代碼

def checksum 
    summation = 0 
    @nmi.reverse.split(//).each_index do |i| 
    value = nmi[nmi.length - i - 1].ord 
    value *= 2 if i.even? 
    value = value.to_s.split(//).map(&:to_i).reduce(:+) 
    summation += value 
    end 
    checksum = (10 - (summation % 10)) % 10 
    checksum 
end 

可能有人請幫我解釋這是什麼意思行?

value = value.to_s.split(//).map(&:to_i).reduce(:+) 

我嘗試將上面的代碼轉換爲VBA for excel。

對於 「4103738516」 的輸入將給你8 「4102030716」 ==> 2 「QFFF0000LV」 ==> 7

在這個文件的第40頁

有JavaScript代碼來計算的話,但我可以不理解代碼。

https://www.aemo.com.au/-/media/Files/PDF/0610-0008-pdf.pdf

謝謝

回答

1

下面的代碼應該讓你明白這句話好:

# Say 
value = 82478923 # any random number 
value.to_s   # => "82478923" 

# `split(//)` is similar to `split` and `split('')`, they split a string after each character to generate an array of characters. 
ary = value.to_s.split(//)  # => ["8", "2", "4", "7", "8", "9", "2", "3"] 

ary.map(&:to_i)  # => [8, 2, 4, 7, 8, 9, 2, 3] 

# `inject(:+)` will iterate the array and calculate sum of all numbers 
ary.map(&:to_i).inject(:+)  # => 43 

瞭解更多關於inject這裏。

+0

謝謝賈迪普:) –

0

如果有人需要,我將此Ruby代碼轉換爲VBA。

用法:Calc_Checksum(NMI)

Function StrReverse(strInput As String) As String 

    Dim strRev As String 
    Dim i As Integer 
    Dim length As Integer 


    strRev = "" 
    length = Len(strInput) 

    For i = 0 To length - 1 
     strRev = strRev & Mid(strInput, length - i, 1) 
    Next i 

    StrReverse = strRev 

End Function 

Function Calc_Checksum(strNMI As String) 

    Dim i As Integer 
    Dim j As Integer 
    Dim x As Integer 
    Dim chrV As Integer 
    Dim tmpStr As String 

    Dim s As Integer 

    s = 0 
    ' Reverse strNMI 
    strNMI = StrReverse(strNMI) 


    'Loop through each char 
    For i = 0 To Len(strNMI) - 1 


     chrV = Asc(Mid(strNMI, i + 1, 1)) 
     'debug.Print chrV 

     If i Mod 2 = 0 Then 
      chrV = chrV * 2 
     End If 

     tmpStr = CStr(chrV) 
     v = 0 
     For j = 1 To Len(tmpStr) 

      v = v + CInt(Mid(tmpStr, j, 1)) 

     Next j 

     s = s + v 

    Next i 


    Calc_Checksum = (10 - (s Mod 10)) Mod 10 

End Function