A data file \"parttolerance.dat\" stores on one line, a part number, and the min
ID: 3109966 • Letter: A
Question
A data file "parttolerance.dat" stores on one line, a part number, and the minimum and maximum values for the valid range that the part could weigh. Write a script "parttol" that will read these values from the file, prompt the user for a weight, and print whether or not that weight is within range. For example. IF the file stores the following: >> type parttolerance.dat 123 44.205 44.287 Here might be examples of executing the script: >> parttol Enter the part weight: 44.33 The part 123 is not in range >> parttol Enter the part weight: 44.25 The part 123 is within rangeExplanation / Answer
Given problem is to write a script "parttol", that
1) will read read the values from "parttolerance.dat" file, which contains in one line, part number, minimum and maximum values for the valid range that part could weigh
2) prompt the user for a weight
----------------------------------- parttol.tcl --------------------------------------
#!/usr/bin/env tclsh
array set partNum_array { }
array set minVal_array { }
array set maxVal_array { }
set fp [open "parttolerance.dat" r]
set partCount 1
while {-1 != [gets $fp line]} {
set numVals [regexp -all -inline {S+} $line]
puts $numVals
set partNum [lindex [split $numVals " "] 0]
set minVal [lindex [split $numVals " "] 1]
set maxVal [lindex [split $numVals " "] 2]
puts $partNum
puts $minVal
puts $maxVal
puts "The current line is $line."
set partNum_array($partCount) $partNum
set minVal_array($partCount) $minVal
set maxVal_array($partCount) $maxVal
set partCount [expr {$partCount + 1}]
}
puts $partNum_array(1)
puts $partNum_array(2)
puts $minVal_array(1)
puts $minVal_array(2)
puts $maxVal_array(1)
puts $maxVal_array(2)
puts "Enter the part weight: "
flush stdout
set srchWeight [gets stdin]
puts $srchWeight
set srchWeight double($srchWeight)
for { set index 1} {$index < $partCount} {incr index} {
set minWgt $minVal_array($index)
set maxWgt $maxVal_array($index)
puts $minWgt
puts $maxWgt
if{ ($srchWeight >= $minWgt) || ($srchWeight <= $maxWgt) } { puts "The part $partNum($index) is within range" }
-------------------------------------------------------------------------------------------
---------------------------------parttolerance.dat------------------------------------
123 44.205 44.287
124 68.125 68.925
---------------------------------------------------------------------------------------------
3) print whether or not the weight is within range
Answer :- Script developed is in