user1270123
user1270123

Reputation: 573

Array handling in TCL

i have an array (the hashmap in TCL), so the command parray dn yields the following

                 dn(1)  = 52638515
                 dn(11) = 324009
                 dn(12) = 257949
                 dn(13) = 298844
                 dn(14) = 442499
                 dn(15) = 417333
                 dn(2)  = 49807360
                 dn(3)  = 52848230
                 dn(4)  = 39845888
                 dn(5)  = 26633830 

so i want this to set it to another array which starts from 1 to length(dn) .. I have several entries in dn like the data above with missing indexes in between. Is there any built in array commands in tcl that does that???

Upvotes: 1

Views: 343

Answers (1)

RHSeeger
RHSeeger

Reputation: 16262

I can't think of an automatic command, but you could do something like the following:

set result {}
set index 0
foreach key [lsort -integer [array keys dn]] {
    lappend result [incr index] $db($key)
}
array set newDn $result

Or, if you have 8.6 and tcllib AND you're glutton for punishment but amusement:

set index 0
array set newDn [struct::list flatten \
                    [::struct::list mapfor x \
                        [dict values [lsort -stride 2 -integer [array get dn]]] \
                        {list [incr index] $x}]]

Though I'd argue that the first version is somewhat easier to follow :)

If tcllib struct::list had a zip command, it would probably be a lot cleaner, since you could zip the sorted (on keys) values with iota 1-n.

Upvotes: 1

Related Questions