﻿ How to count how many keys are duplicated in global? | InterSystems
Question
· Jul 17, 2019

# How to count how many keys are duplicated in global?

Hi there

Would you please advise how to count how many keys are duplicated in global, for example, Athens was duplicate 3.

Set ^Data("Cambridge") = "1"
Set ^Data("New York") = "2"
Set ^Data("Boston") = "3"
Set ^Data("London") = "4"
Set ^Data("Athens") = "5"
Set ^Data("Athens") = "6"
Set ^Data("Athens") = "7"

Thanks

Discussion (9)0

Globals can't have duplicate keys.

After I execute your code and call

``zw ^Data ``

I get this output:

``````^Data("Athens")=7
^Data("Boston")=3
^Data("Cambridge")=1
^Data("London")=4
^Data("New York")=2``````

Did you try to set it as is?

There is no way to get duplication as you describe.

You can check If there is already the same value with \$DATA function.

`Write \$Data(^Data("Athens"))`

Where have you seen such a global?

Keys in global are unique. If you set the value of a global for the same key you'll override the value for the key.

Like @Eduard Lebedyuk wrote if you run 3 commands:

Set ^Data("Athens") = "5"
Set ^Data("Athens") = "6"
Set ^Data("Athens") = "7"

You'll first have 5, then override it with 6 and end up with value 7 for the key "Athens".

to count the number of changes you may use this approach

`````` Set ^Data("Cambridge") = "1" if  \$increment(change("Cambridge"))
Set ^Data("New York") = "2" if  \$increment(change("New York"))
Set ^Data("Boston") = "3" if  \$increment(change("Boston"))
Set ^Data("London") = "4" if  \$increment(change("London"))
Set ^Data("Athens") = "5" if  \$increment(change("Athens"))
Set ^Data("Athens") = "6" if  \$increment(change("Athens"))
Set ^Data("Athens") = "7" if  \$increment(change("Athens"))

zw change

change("Athens")=3
change("Boston")=1
change("Cambridge")=1
change("London")=1
change("New York")=1``````

Global keys are always unique! I think you are confused because in a traditional sql environment you can only insert a row using a key and value pair once. After that, to change the value you do an update!

In cache, there is no distinction between setting a global node the first, the second or the third time.

insert into student(studentid, name) values(1, 'john')

to change this, you must do an update:

update student set name = 'john smith' where studentid  = 1

in cache, you can simply do this:

set ^student(1)="john"

and then do:

set ^student(1)="john smith"

There is no obvious distinction between n insert and an update!