| View previous topic :: View next topic |
| Author |
Message |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Mon Apr 17, 2017 1:41 pm Post subject: |
|
|
As expected. The -stride option has been added in 8.6, hence the error.
Try with the change i said above. _________________ Once the game is over, the king and the pawn go back in the same box. |
|
| Back to top |
|
 |
juanamores Master
Joined: 15 Mar 2015 Posts: 317
|
Posted: Mon Apr 17, 2017 1:42 pm Post subject: |
|
|
Error: Tcl error [averages]: element 1 missing from sublist "10" _________________ If you do not understand my ideas is because I can not think in English, I help me with Google Translate. I only speak Spanish. Bear with me. Thanks  |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Mon Apr 17, 2017 1:56 pm Post subject: |
|
|
Yeah, had a feeling that will fail miserably.
Here is a TCL 8.4/8.5 version that should be compatible with newer versions of TCL, if you decide to install a new one (a good idea security wise by the way).
| Code: |
bind pub - !avgs averages
proc averages {nick uhost hand chan text} {
set fp [open "ranking" "r"]
set data [read -nonewline $fp]
close $fp
foreach line [split $data "\n"] {
if {[scan $line {%s%d} name rank] != 2} continue
lappend ranking($name) $rank
}
foreach {name values} [array get ranking] {
set average [expr ([join $values +]) / [llength $values]]
lappend result [list $name $average]
}
set results [lsort -index 1 -integer -decreasing [lsort -index 0 $result]]
foreach pair $results {
scan $pair {%s%d} name rank
puts "$name $rank"
}
}
|
Result:
| Quote: |
Maria 12
Jose 10
Ana 9
Pedro 7
Ben 3
Luis 3
|
_________________ Once the game is over, the king and the pawn go back in the same box. |
|
| Back to top |
|
 |
juanamores Master
Joined: 15 Mar 2015 Posts: 317
|
Posted: Mon Apr 17, 2017 2:20 pm Post subject: |
|
|
caesar is almost perfect.
There is only one small detail that I can not solve.
It is about applying the rounding rule with the "round" function.
If you look at the values of Pedro
9 + 10 + 4 = 23
23/3 = 7.66
The final result of Pedro should be 8, not 7.
It can be fixed? _________________ If you do not understand my ideas is because I can not think in English, I help me with Google Translate. I only speak Spanish. Bear with me. Thanks  |
|
| Back to top |
|
 |
juanamores Master
Joined: 15 Mar 2015 Posts: 317
|
Posted: Mon Apr 17, 2017 4:17 pm Post subject: [Solved!] |
|
|
Fixed! This is what I wanted.
Thank you so much to caesar and willyw.
| Code: |
proc averages {nick uhost hand chan text} {
set fp [open "ranking" "r"]
set data [read -nonewline $fp]
close $fp
foreach line [split $data "\n"] {
if {[scan $line {%s%d} name rank] != 2} continue
lappend ranking($name) [format "%.2f" $rank]
}
foreach {name values} [array get ranking] {
set average [expr ([join $values +]) / [llength $values]]
lappend result [list $name [expr round ([format "%.2f" $average])]]
#putlog "debugging decimal: $average"
}
set results [lsort -index 1 -real -decreasing [lsort -index 0 $result]]
foreach pair $results {
scan $pair {%s%d} name rank
putlog "$name $rank"
}
}
|
_________________ If you do not understand my ideas is because I can not think in English, I help me with Google Translate. I only speak Spanish. Bear with me. Thanks  |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Tue Apr 18, 2017 12:49 am Post subject: |
|
|
Since the rank you read from the file is an integer you don't really need to format it to real cos when doing the average it won't matter if it was real or not.
Anyway, glad you sorted it out. De nada.  _________________ Once the game is over, the king and the pawn go back in the same box. |
|
| Back to top |
|
 |
juanamores Master
Joined: 15 Mar 2015 Posts: 317
|
Posted: Tue Apr 18, 2017 6:14 am Post subject: |
|
|
You're right, I've changed it back to -integer and it works fine.
I had changed it to -real because earlier it gave me an error of the type:
Tcl error: expected floating-point number but got "5.00 15.00 11.00 14.00 2.00".
I understood that a floating number was expected and got a list of floating numbers.
Now I changed it again as you advise me and it works ..
It's just that I made so many changes and tests that I forgot about the mistakes it gave me.
Thank you so much caesar  _________________ If you do not understand my ideas is because I can not think in English, I help me with Google Translate. I only speak Spanish. Bear with me. Thanks  |
|
| Back to top |
|
 |
|