Accessing list of list of lists

Viewed 61

Having

set x {{{a b} {c d}}}

I would like to read "a". Is there a cleaner way than:

lindex [lindex [lindex $x 0] 0] 0
1 Answers

The lindex command supports two other ways of doing that access:

  1. Multiple keys:

    lindex $x 0 0 0
    
  2. Compound keys:

    lindex $x {0 0 0}
    

You can't mix the two in the same call; compound keys only work if they're the sole index argument. Just to keep a little bit of comprehensibility, both for people using the command and people implementing it.

The lset command supports the same indexing schemes.

Related