Search code examples
arrayspseudocodemumps

Grouping an array


PLEASE...Help me group the values in this array

I have a an array like this

    Arr(0) = 5
    Arr(1) = ATC^1
    Arr(2) = BTC^2
    Arr(3) = ATC^3
    Arr(4) = CTC^4
    Arr(5) = BTC^5

The end result I want is to change Arr(0) to be 3 which are the number of unique TC's in the group. ATC,BTC and CTC. And I want each of the items for 1,2 and 3 to have the grouped values from the Array.

    Arr(0) = 3
    Arr(1) = ATC:1,3
    Arr(2) = BTC:2,5
    Arr(3) = CTC:4

I am trying to do this in MUMPs, so I have no inbuilt sort/group functions.

Even pseudo code will help.


Solution

  • In MUMPS you don't need sort function because array subscripts are being automatically sorted.

    So the easiest way is to create another array with sort dimension being first piece of data and value - second piece of data:

    for i=1:1:Arr(0) set piece1=$piece(Arr(i),"^",1), piece2=$piece(Arr(i),"^",2), Temp(piece1)=$get(Temp(piece1))_piece2_","
    

    after running that code you will get following array

    Temp("ATC")="1,3,"
    Temp("BTC")="2,5,"
    Temp("CTC")="4,"
    

    Then you traverse that array and build one you need:

    set i="",Brr=0 for  set i=$o(Temp(i)) quit:i=""  set Brr=Brr+1,Brr(Brr)=i_":"_Temp(i)
    

    You will get an array you need:

    Brr(1)="ATC:1,3,"
    Brr(2)="BTC:2,5,"
    Brr(3)="CTC:4,"