none
Array Problem RRS feed

  • Question

  • Hi all, I apologize for my English, I'm using google translator
    I have two arrays, codOV [i] and qnt [i], and if I find identical elements within the array codOV [i] I have to make the sum of the elements in the array qnt [i].
    Put simply if both arrays contain the following elements:
    codOV[i]:{gradone,book,scale,gradone,book}; qnt[i]:{1,1,1,1,2}

    I should get the new array in the following form:
    codOV[i]:{gradone,book,scale} qnt[i]:{2,3,1}

    I hope that everything is clear and you can help

    T.U.

    Monday, January 23, 2017 3:05 PM

Answers

  • And another way: Basicaly step 1 does it all

    For i = 1 To Array.GetItemCount(odOV)
      temp[odOV[i]] = temp[odOV[i]]+qnt[i]
    EndFor

    And a full program

    'ORIGINAL ARRAYS
    odOV = "1=gradone;2=book;3=scale;4=gradone;5=book;"
    qnt = "1=1;2=1;3=1;4=1;5=2;"
    TextWindow.WriteLine("odOV : "+odOV)
    TextWindow.WriteLine("qnt  : "+qnt)
    TextWindow.WriteLine("")
    
    'STEP 1
    temp = ""
    For i = 1 To Array.GetItemCount(odOV)
      temp[odOV[i]] = temp[odOV[i]]+qnt[i]
    EndFor
    TextWindow.WriteLine("temp : "+temp)
    
    'STEP 2
    indices = Array.GetAllIndices(temp)
    odOV = ""
    qnt = ""
    For i = 1 To Array.GetItemCount(temp)
      index = indices[i]
      odOV[i] = index
      qnt[i] = temp[index]
    EndFor
    TextWindow.WriteLine("odOV : "+odOV)
    TextWindow.WriteLine("qnt  : "+qnt)
    
    'OUTPUT RESULT
    TextWindow.WriteLine("")
    For i = 1 To Array.GetItemCount(odOV)
      TextWindow.WriteLine(odOV[i]+" : "+qnt[i])
    EndFor
    TextWindow.WriteLine("")

    • Marked as answer by Carmenello Wednesday, January 25, 2017 10:36 AM
    Tuesday, January 24, 2017 6:46 PM
    Moderator
  • I wrote an algorithm, and it seems to work, I will forward to any future request and also to have it checked to you = P


    For i = 1 To Array.GetItemCount(codOV)
        If(Array.ContainsValue(cc,codiOV[i])) Then
          Goto exit
        EndIf
        cc[i] = codOV[i]
        For j = i+1 To Array.GetItemCount(codOV)
          If(codOV[i] = codOV[j]) Then
            quantity[i] = quantity[i] + qnt[j]
          EndIf
        EndFor
        quantity[i] = quantity[i] + qnt[i]
        exit:
      EndFor

    For i = 1 To Array.GetItemCount(cc)
      GraphicsWindow.ShowMessage(cc[i]+"  "+quantity[i],"")
    EndFor

    • Marked as answer by Carmenello Tuesday, January 24, 2017 3:14 PM
    Tuesday, January 24, 2017 8:59 AM

All replies

  • What code do you have so far?

    Hint:

    1] loop though both arrays at the same time, creating a new array indexed by codOV with values set to sum of qnt

    2] get the new array indices to back create the new codOV and qnt

    Monday, January 23, 2017 6:39 PM
    Moderator
  • I wrote an algorithm, and it seems to work, I will forward to any future request and also to have it checked to you = P


    For i = 1 To Array.GetItemCount(codOV)
        If(Array.ContainsValue(cc,codiOV[i])) Then
          Goto exit
        EndIf
        cc[i] = codOV[i]
        For j = i+1 To Array.GetItemCount(codOV)
          If(codOV[i] = codOV[j]) Then
            quantity[i] = quantity[i] + qnt[j]
          EndIf
        EndFor
        quantity[i] = quantity[i] + qnt[i]
        exit:
      EndFor

    For i = 1 To Array.GetItemCount(cc)
      GraphicsWindow.ShowMessage(cc[i]+"  "+quantity[i],"")
    EndFor

    • Marked as answer by Carmenello Tuesday, January 24, 2017 3:14 PM
    Tuesday, January 24, 2017 8:59 AM
  • And another way: Basicaly step 1 does it all

    For i = 1 To Array.GetItemCount(odOV)
      temp[odOV[i]] = temp[odOV[i]]+qnt[i]
    EndFor

    And a full program

    'ORIGINAL ARRAYS
    odOV = "1=gradone;2=book;3=scale;4=gradone;5=book;"
    qnt = "1=1;2=1;3=1;4=1;5=2;"
    TextWindow.WriteLine("odOV : "+odOV)
    TextWindow.WriteLine("qnt  : "+qnt)
    TextWindow.WriteLine("")
    
    'STEP 1
    temp = ""
    For i = 1 To Array.GetItemCount(odOV)
      temp[odOV[i]] = temp[odOV[i]]+qnt[i]
    EndFor
    TextWindow.WriteLine("temp : "+temp)
    
    'STEP 2
    indices = Array.GetAllIndices(temp)
    odOV = ""
    qnt = ""
    For i = 1 To Array.GetItemCount(temp)
      index = indices[i]
      odOV[i] = index
      qnt[i] = temp[index]
    EndFor
    TextWindow.WriteLine("odOV : "+odOV)
    TextWindow.WriteLine("qnt  : "+qnt)
    
    'OUTPUT RESULT
    TextWindow.WriteLine("")
    For i = 1 To Array.GetItemCount(odOV)
      TextWindow.WriteLine(odOV[i]+" : "+qnt[i])
    EndFor
    TextWindow.WriteLine("")

    • Marked as answer by Carmenello Wednesday, January 25, 2017 10:36 AM
    Tuesday, January 24, 2017 6:46 PM
    Moderator