Getting started with Tcl: example 14

This page is part of the Airplug documentation related to Tcl/Tk.
Back to the Tcl/Tk documentation page / Back to the Airplug documentation page

How to sort a list

Script

#!/usr/bin/tclsh
# Scanning arrays using for
 
set lst [split "b:a:b:c:a:b:d" ":"]
 
puts stdout "original list: $lst"
 
puts stdout "list without repetitive term but with a different order: [lsort -unique $lst]"
 
set i 0
foreach item $lst {
 		if { [ info exists tab($item) ] } {
				set lst [lreplace $lst $i $i]
 		} else {
 				incr i
		}
		incr tab($item)
}
 
 
puts stdout "list without repetitive terms in the same order: $lst"
puts stdout "array of the numnber of occurences: [array get tab]"
puts stdout "number of occurences of [lindex $lst 1] = $tab([lindex $lst 1])"

Output

original list: b a b c a b d
list without repetitive term but with a different order: a b c d
list in the same order but without repetitive terms: b a c d
array of the numnber of occurences: d 1 a 2 b 3 c 1
number of occurences of a = 2

<br/>


This page is part of the Airplug documentation related to Tcl/Tk.
Back to the Tcl/Tk documentation page / Back to the Airplug documentation page

 
Back to top
en/doc/pub/tcltk/example-14.txt · Last modified: 2017/04/05 16:17 (external edit)
 
 
Driven by DokuWiki