A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Linear Search in Tcl page! Here, you'll find the source code for this program as well as a description of how the program works.
proc usage {} {
puts stderr {Usage: please provide a list of integers ("1, 4, 5, 11, 12") and the integer to find ("11")}
exit 1
}
proc parseList {s} {
set tokens [split [string trim $s] ","]
if {[llength $tokens] < 1} { usage }
set result {}
set result {}
foreach token $tokens {
set t [string trim $token]
if {$t eq "" || [catch {expr {int($t)}} val]} usage
lappend result $val
}
return $result
}
proc parseInt {s} {
set s [string trim $s]
if {$s eq "" || [catch {expr {int($s)}} val]} {
usage
}
return $val
}
proc linearSearch {nums value} {
expr {[lsearch -integer -exact $nums $value] != -1}
}
if {$argc != 2} { usage }
set numbers [parseList [lindex $argv 0]]
set key [parseInt [lindex $argv 1]]
set found [linearSearch $numbers $key]
puts [expr {$found ? "true" : "false"}]
Linear Search in Tcl was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.