Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
687 views
in Technique[技术] by (71.8m points)

hashmap - Duplicate keys in tcl associative array

I am using associative array/hash map to store some data. I want to keep duplicate entries that have same key but different values. Currently, the previous key is being overwritten by the last instance.

key: LVL values: vdd,vddr

key: LVL values: vddi,vdd


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Multimaps are, in general, implemented as ordinary maps to lists of items. (There's a few other ways, algorithmically, but they're rare in practice except in a few degenerate cases.) In Tcl, you do this by appending to the list in the element on creation and using nested loops on read out:

# With arrays
foreach {key item} $thingsToPutIn {
    lappend map($key) $item
}

foreach {key items} [array get map] {
    foreach item $items {
        puts "$key => $item"
    }
}
# With dictionaries
foreach {key item} $thingsToPutIn {
    dict lappend map $key $item
}

dict for {key items} $map {
    foreach item $items {
        puts "$key => $item"
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...