I think I solved your questions regarding your two issues:
(Note: I did test this!)
1.)
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
2.)
what if I don't know exact numbers?
num1
num2
num3
This works great, give it a try ! I've just appended code to your previous code, beginning at Start of cptk's
set readFile [open "listsort.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"
#set lineList2 [split $lineList1 " "]; #make a proper list
lappend storeList $lineList1; #list of lists
gets $readFile line
}
#now work on storeList
#set openList [lsort -index 1 $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]"
##### Start of cptk's #####
set StoreName ""
set StoreId ""
set StoreNum ""
for {set i 0} {$i < [llength $storeList]} {incr i} {
I got this answer
1 10:10 16:00
2 11:10 17:00
3 11:20 18:00
here is a code.
but if you have any good idea, please advise me.
if we don't know "num", let say 1000
can I use "while" loop?
I think..I need to change this loop to for-loop
Do you have any idea??
thank you
---------------
foreach elm $storeList {
switch [lindex $elm 0] {
1 {lappend sublist1 $elm}
2 {lappend sublist2 $elm}
3 {lappend sublist3 $elm}
}
}
---------------------------
Thank you all
---b.txt---
num open close
1 "10:10" "12:00"
1 "12:10" "14:00"
1 "14:10" "16:00"
2 "11:10" "13:00"
2 "14:10" "16:00"
2 "15:10" "17:00"
3 "11:20" "12:00"
3 "12:10" "13:00"
3 "14:10" "18:00"
-------------------
set readFile [open "b.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"
lappend storeList $lineList1; #list of lists
gets $readFile line
}
puts "#######"
foreach elm $storeList {
switch [lindex $elm 0] {
1 {lappend sublist1 $elm}
2 {lappend sublist2 $elm}
3 {lappend sublist3 $elm}
}
}
#--------num1
set openListA [lsort -index 1 $sublist1]
set openA "[lindex $openListA 0]"
set openAindex1 [lindex $openA 0];#house number
set openAindex2 [lindex $openA 1];#earliest opening time
set closeListA [lsort -index 2 -decreasing $sublist1]
set closeA "[lindex $closeListA 0]"
set closeAindex1 [lindex $closeA 2];#earliest opening time
puts "The result A : $openAindex1 $openAindex2 $closeAindex1"
#--------num2
set openListB [lsort -index 1 $sublist2]
set openB "[lindex $openListB 0]"
set openBindex1 [lindex $openB 0];#house number
set openBindex2 [lindex $openB 1];#earliest opening time
set closeListB [lsort -index 2 -decreasing $sublist2]
set closeB "[lindex $closeListB 0]"
set closeBindex1 [lindex $closeB 2];#earliest opening time
puts "The result B : $openBindex1 $openBindex2 $closeBindex1"
#--------num3
set openListC [lsort -index 1 $sublist3]
set openC "[lindex $openListC 0]"
set openCindex1 [lindex $openC 0];#house number
set openCindex2 [lindex $openC 1];#earliest opening time
set closeListC [lsort -index 2 -decreasing $sublist3]
set closeC "[lindex $closeListC 0]"
set closeCindex1 [lindex $closeC 2];#earliest opening time
puts "The result C : $openCindex1 $openCindex2 $closeCindex1"
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.