If you want the first digit preceeded by a, the second digit preceeded by b,.. , here is a solution:
Code:
set num 1234.56
set letters {a b c d e f}
set l -1
set res {}
foreach n [split $num ""] {
if {$n != "."} {
lappend res [list [lindex $letters [incr l]] $n]
}
}
puts [join $res \n]
With my bad english, I'll try to explain what this do.
Splitting the digits from 'num' is matter of:
Code:
[split $num ""]
.
The "" indicates that splitting occurs each "" separator, that is: at each char.
The 'l' var with value -1 is the index in the letters list.
will return 0 the first time it will be evaled, 1 the second time,..
The 'res' var with empty value will contain the result.
Code:
foreach n [split $num ""]
will eval the following body with n valued to "1", then "2", then...
The complex
Code:
lappend res [list [lindex $letters [incr l]] $n]
is not so hard to understand:
gives the succeeding indexes in the letters list.
Code:
[lindex $letters [incr l]]
gives the succeeding letters in the list.
Code:
[list [lindex $letters [incr l]] $n]
creates a two items list: the selected letter and the current digit.
And
Code:
lappend res [list [lindex $letters [incr l]] $n]
appends the two items list to the result.
During the process the "." is discarded.
Finally, the result is printed prettyfied by
that insert a newline between the two items lists.
Good luck
ulis