Baja
Baja

Reputation: 1

Finding "lsort" indices in .tcl

How can we obtain indices of lsort?

For example:

lsort -real {1 -4 6 0}

how can I obtain indices for the code above as idx = (1, 3, 0, 2)?

Upvotes: 0

Views: 233

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137587

The -indices option to lsort does exactly what you want:

set values {1 -4 6 0}
set indices [lsort -indices -real $values]
foreach idx $indices {
    puts "[lindex $values $idx] is at $idx"
}

Output:

-4 is at 1
0 is at 3
1 is at 0
6 is at 2

Upvotes: 4

Related Questions