How to increment each element in a list?
eg: incrlist {1 2 3}
=>2 3 4
Prog #1
proc incrlist args {
set s 0
foreach s $args {
incr s 1
puts $s
}
}
//for list
proc incrlist list {
set s 0
foreach s $list {
incr s 1
puts $s
}
}
Prog #2
proc incrlst lst {
set lst [lreplace $lst 0 0]
lappend lst [expr [lindex $lst ([llength $lst]-1)]+1]
}
incrlst \\"3 4 5\\"
Reverse a string in tcl
eg : reverse 12345
==> 54321
Prog #1
//reverse.exp
set a 12345
set b [string length $a]
puts $b
for {set i $b} {$i >= 0} {incr i -1} {
puts -nonewline [string index $a [expr $i -1]]
}
tcl exec :
tclsh reverse.exp
54321
Prog #2
//strrev.exp
set string "tclprogram"
set rev [string reverse $string]
puts $rev
tcl exec:
tclsh strrev.exp
mragorplct
Reverse a list in tcl
Prog #1
set list "1 3 137 18 6 30 11"
set len [llength $list]
for {set i $len} {$i > 0} {incr i -1} {
set b [lindex $list [expr $i - 1]]
puts -nonewline "$b "
}
Prog #2
set lst {1 2 3 30 -1 a b}
puts [lreverse $lst]
eg: incrlist {1 2 3}
=>2 3 4
Prog #1
proc incrlist args {
set s 0
foreach s $args {
incr s 1
puts $s
}
}
//for list
proc incrlist list {
set s 0
foreach s $list {
incr s 1
puts $s
}
}
Prog #2
proc incrlst lst {
set lst [lreplace $lst 0 0]
lappend lst [expr [lindex $lst ([llength $lst]-1)]+1]
}
incrlst \\"3 4 5\\"
Reverse a string in tcl
eg : reverse 12345
==> 54321
Prog #1
//reverse.exp
set a 12345
set b [string length $a]
puts $b
for {set i $b} {$i >= 0} {incr i -1} {
puts -nonewline [string index $a [expr $i -1]]
}
tcl exec :
tclsh reverse.exp
54321
Prog #2
//strrev.exp
set string "tclprogram"
set rev [string reverse $string]
puts $rev
tcl exec:
tclsh strrev.exp
mragorplct
Reverse a list in tcl
Prog #1
set list "1 3 137 18 6 30 11"
set len [llength $list]
for {set i $len} {$i > 0} {incr i -1} {
set b [lindex $list [expr $i - 1]]
puts -nonewline "$b "
}
Prog #2
set lst {1 2 3 30 -1 a b}
puts [lreverse $lst]
No comments:
Post a Comment