Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
package require Tcl 8.2
package require math
proc thislisttoarray {lst arrayname} {
global [subst -novariable $arrayname]
array set [subst -novariable $arrayname] {}
foreach {x y} $lst {set [subst -novariable $arrayname]($x) $y}
}
proc myqsort {arrayname min max {function "string compare"}} {
upvar $arrayname local
if {$min > $max} {return}
set m $min
for {set x [expr $min + 1]} {$x <= $max} {incr x} {
if {[eval $function $local($x) $local($min)] < 0} {
swap local [incr m] $x
}
}
swap local $min $m
myqsort local $min [expr $m - 1]
myqsort local [expr $m + 1] $max
return
}
proc swap {arr el1 el2} {
upvar 1 $arr locswap
set sw $locswap($el1)
set locswap($el1) $locswap($el2)
set locswap($el2) $sw
return
}
set lst {4 10:30 5 14:25 1 00:12 2 03:30 3 08:30}
thislisttoarray $lst aname
parray aname
aname(1) = 00:12
aname(2) = 03:30
aname(3) = 08:30
aname(4) = 10:30
aname(5) = 14:25
myqsort aname 1 [array size aname]
Now let's take your code from above and add some new lines:-index index
If this option is specified, each of the elements of list must itself be a proper Tcl sublist. Instead of sorting based on whole sublists, lsort will extract the index'th element from each sublist and sort based on the given element. The keyword end is allowed for the index to sort on the last sublist element, and end-index sorts on a sublist element offset from the end. For example,
lsort -integer -index 1 {{First 24} {Second 18} {Third 30}}
returns {Second 18} {First 24} {Third 30}, and
lsort -index end-1 {{a 1 e i} {b 2 3 f g} {c 4 5 6 d h}}
returns {c 4 5 6 d h} {a 1 e i} {b 2 3 f g}. This option is much more efficient than using -command to achieve the same effect.
set readFile [open "test.txt" "r"]
gets $readFile line
while {![eof $readFile]} {
# write the line to the backup file "output this line to a file"
set lineList1 [string map {\" ""} $line]
set storeNum [lindex $lineList1 0]
set startTime [lindex $lineList1 1]
set closeTime [lindex $lineList1 2]
puts "$storeNum $startTime $closeTime"
[red]
set lineList2 [split $lineList1 " "]; #make a proper list
lappend storeList $lineList2; #list of lists
[/red]
gets $readFile line
}
[red]
#now work on storeList
set openList [lsort -index 1 $storeList]
puts "earliest opening time = [lindex $openList 0]
set closeList [lsort -index 2 -decreasing $storeList]
puts "latest closing time = [lindex $closeList 0]
[/red]
I think I'd make a couple of sorting passes. First, lsort with -index 0. That will group all the stores by store number. Then go through the list with foreach and make a sublist for each store:if I want to have
num 1's earliest time and lasted time
num 2's earliest time and lasted time
num 3's earliest time and lasted time
can I use for-loop??
foreach elm $<listname> {
switch [lindex $elm 0] {
1 {lappend sublist1 $elm}
2 {lappend sublist2 $elm}
...
}
}
what if I don't know exact numbers?
num1
num2
num3
....
num?