This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Sort file data

Help for those learning Tcl or writing their own scripts.
User avatar
caesar
Mint Rubber
Posts: 3776
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

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.
j
juanamores
Master
Posts: 317
Joined: Sun Mar 15, 2015 9:59 am

Post by juanamores »

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 :)
User avatar
caesar
Mint Rubber
Posts: 3776
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Yeah, had a feeling that will fail miserably. :roll:

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: Select all

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:
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.
j
juanamores
Master
Posts: 317
Joined: Sun Mar 15, 2015 9:59 am

Post by juanamores »

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 :)
j
juanamores
Master
Posts: 317
Joined: Sun Mar 15, 2015 9:59 am

[Solved!]

Post by juanamores »

Fixed! This is what I wanted. :) :) :) :)
Thank you so much to caesar and willyw.

Code: Select all

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 :)
User avatar
caesar
Mint Rubber
Posts: 3776
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

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.
j
juanamores
Master
Posts: 317
Joined: Sun Mar 15, 2015 9:59 am

Post by juanamores »

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. :lol:

Thank you so much caesar :D
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 :)
Post Reply