Linear Search in Tcl

Published on 09 October 2025 (Updated: 09 October 2025)

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.

Current Solution

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.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.