Hello World:
A list of simple examples showing what Hecl looks like in action.
puts "Hello, World"
Set a variable, print it out in the midst of a string:
set rating 10
puts "Hecl, from 1 to 10: $rating"
Grouping with braces {}:
puts {The $dollar $signs $are printed literally$$ - no substitution}
Utilize the result of a command:
set rating 10
puts "Rating:"
puts [set rating]
Basic math:
puts "2 + 2 = [+ 2 2]"
"if" command:
set temp 10
if { < $temp 0 } {
puts "It's freezing"
} else {
puts "Not freezing"
}
"while" loop command:
set i 0
while { < $i 10 } {
puts "i is now $i"
incr $i
}
Lists:
set foo [list a b c]
set bar {a b c}
lappend $foo d
lappend $bar d
set foo
# Returns 'a b c d'
set bar
# Returns 'a b c d'
Hash tables:
set foo [hash {a b c d}]
puts [hget $foo a]
# prints 'b'
puts [hget $foo c]
# prints 'd'
hset $foo c 2
puts [hget $foo c]
# prints '2'
puts $foo
# prints 'a b c 2' (although not necessarily in that order)
"foreach" loop command:
set lst {a b c d e f}
foreach {m n} $lst {
puts "It is possible to grab two variables at a time: $m $n"
}
foreach {x} $lst {
puts "Or one at a time: $x"
}
Create new commands with the "proc" command. In this example we create a command that prints out a numbered list.
set list {red blue green}
proc printvals {vals} {
set num 1
foreach v $vals {
puts "$num - $v"
incr $num
}
}
printvals $list
Hecl is very flexible - in this example, we create a "do...while" loop command that works as if it were a native loop construct.
proc do {code while condition} {
upeval $code
while { upeval $condition } {
upeval $code
}
}
set x 100
set foo ""
do {
append $foo $x
incr $x
} while {< $x 10}
set foo
# Returns 100 - because the loop is run once and only once.