Question

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.

Was it helpful?

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,"

OTHER TIPS

Alternate:

Two dimension array:

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

The $o loop can look through the first level for the items in sorted order, then a nested $o to run through the second. Or, just the first if you only want the sorted list. This would save the string logic if you are going to selectively remove elements from the list and just let you set and kill notes in the array.

Sorting logic is uncommon with MUMPS, data is normally loaded/stored into arrays like which keeps them in sorted order.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top