4 ms·
foreach line [split $file \n] { incr count($line) } foreach {line n} [array get count] { if {$n > 1} {
by treis 3y ago
foreach line [split $file \n] {
incr count($line)
}
foreach {line n} [array get count] {
if {$n > 1} {
puts "$n\t$line"
}
}
......
IMHO that's hard to grok. count shows up in the middle of a loop and it's not clear to me what it is. Could be a function, an integer, a map, or an array.
Same problem with this:
>foreach {line n} [array get count]
Not really obvious what any of this does. Guessing it's taking a map, converting it to key/value array, and iterating.
- pwg 3y ago> Could be a function, an integer, a map, or an array. It is an array. 'incr' is a built in function that "increments" a variable by one. Variable names followed by parenthesis () are Tcl arrays. So count(...) is "array syntax". > Same problem with this: > >foreach {line n} [array get count] This is roughly equivalent to Python's "list comprehensions". "array get count" produces a list of values from the contents of the array named count (the list is "key1 value1 key2 value2 key3 value3 ..." foreach is Tcl's "iterate over a list" function. In this case it is iterating two variables over the list at the same time taking two elements off the list each time. The first element is assigned to "line" the second element to "n".
- BeetleB 3y ago> IMHO that's hard to grok. Keep in mind that the default way C does for loops is the most un-intuitive way of doing a for loop. This Tcl example is less alien than the C for loop is for someone who has programmed in non-C like languages. You learn it, and you get used to it.
- msla 3y agofor dish, price := range menu { fmt.Println(dish, price) } for key, value in menu.items(): print(key, ' is ', value) for (key, value) in hashmap { println!("{} {}", key, value); } Really, it seems like that's a common syntax for languages with first-class dictionaries.