# HelloWorld013.tcl - 2015-06-24
# http://i...content-available-to-author-only...e.com/1t0KK7
# foreach, unicode-chars, append, lappend, array,parray, dict
proc dumpArray {A} {
# show contents of an array
puts "dumpArray:"
foreach key [array names A] {
set val $A($key)
puts "$key : $val"
}
}
set deckStr "" ;# create empty string
set deckList {} ;# create empty list
array set deckArray {} ;# create empty array
puts "Deck of cards:"
set i 0
# see also: http://w...content-available-to-author-only...l.tk/26403 : [HTML character entity references]
# Spades Hearts Diamonds Clubs
foreach {suit sym} { S \u2660 H \u2665 D \u2666 C \u2663 } {
foreach rank { A 2 3 4 5 6 7 8 9 10 J Q K } { ;# Ace 2 .. 10 Jack Queen King
incr i
set card "$rank$sym"
puts -nonewline " $rank$suit=$card "
append deckStr $card ;# add card to string
lappend deckList $card ;# add card to list
set deckArray($i) $card ;# add card to array
}
puts ""
}
set maxCard $i
puts "\ndeckStr : $deckStr" ;# note: no spaces between
puts "\ndeckList : $deckList"
puts "\nparray:"
parray deckArray ;# show all elements, unsorted
puts "Done."
exit
###
puts "\ndeckArray (numeric): "
# This works for numeric keys only:
for {set i 1; set n 1} {$i<=$maxCard} {incr i; incr n} {
puts -nonewline "$i:$deckArray($i) "
if { $n >= 13 } { puts ""; set n 0 } ;# newline
}
#dumpArray $deckArray
puts "\ndeckArray (any): "
# This works for any type of keys, but with no specific order:
set n 0
foreach key [array names deckArray] {
incr n
set val $deckArray($key)
puts -nonewline "$key:$val "
if { $n >= 8 } { puts ""; set n 0 } ;# newline
}
puts "\nDone."
exit
puts "\nThe list with the deck of cards has [llength $deck] elements:"
for {set i 0; set n 0} {$i < [llength $deck]} {incr i; incr n} {
if { $n >= 8 } { puts ""; set n 0 } ;# newline after 8 cards
if { $n == 4 } { puts -nonewline " " } ;# extra space after 4 cards
set c [lindex $deck $i] ;# get element #i from the list
puts -nonewline "$i: $c "
}
puts "\nDone."
#.