83218

Is there shorthand in Tcl to get a sequential array of numbers?

Question:

For example, in Perl, to get a sequential array of numbers from 1 to 10, you could simply do:

@myArray = (1 .. 10);

The two periods serve as shorthand for this operations instead of making a for loop or writing the whole thing out manually. Other languages I've used have something similar also.

Does a similar shorthand exist in Tcl?

Answer1:

Not quite this one, but

% package require struct::list 1.6.1 % struct::list iota 10 0 1 2 3 4 5 6 7 8 9

Also search <a href="http://wiki.tcl.tk/819" rel="nofollow">this</a> for the "iota" keyword to see how this can be done using a one-liner.

Answer2:

With the exception of expressions (which are their own little language) Tcl has no operators and is always a strictly prefix-driven language. This means that there isn't such a convenient shorthand for doing loops. On the other hand, there's nothing particularly special about Tcl's standard commands (apart from some minor efficiency details that don't matter here) so making your own is no problem:

proc .. {from to} { if {$from >= $to} { for {set i $from} {$i <= $to} {incr i} {lappend out $i} } else { for {set i $from} {$i >= $to} {incr i -1} {lappend out $i} } return $out } puts [.. 1 10]; # --> “1 2 3 4 5 6 7 8 9 10”

You can fake infix operators by using an unknown handler (as in GrAnd's answer) but that's really quite slow by comparison with the above.

Answer3:

You can define the method:

proc fillArray {a b} { eval return \[list $a [string repeat "\[incr a\] " [incr b -$a]]\] }

And use it as:

set myArray [fillArray 1 10] <hr />

You even can beautify the call of procedure to make it look as in perl. For that just redefine unknown procedure:

rename unknown __unknown proc unknown {args} { if {[llength $args] == 3} { lassign $args a op b if {[string is integer $a] && $op == ".." && [string is integer $b]} { return [fillArray $a $b] } } return [uplevel __unknown {*}$args] }

After that you can write just simple as:

set myArray [1 .. 10]

:)

Answer4:

No, a similar shorthand does not exist in tcl.

If you really want shorthand, you can create your own command that looks almost the same. For example:

proc : {start ignore end} { set result [] for {set i $start} {$i <= $end} {incr i} { lappend result $i } return $result } puts "from 1 to 10: [: 1 .. 10]"

Recommend

  • Access to filesystem in a Mac App JSContext
  • Update table column values based on conditional logic
  • Spring boot application configuration question
  • Camel example can't find org.apache.camel.impl.DefaultComponent
  • Using defer with pointers
  • Excel like column operations in R dataframe
  • int -> int list is not compatible with type int -> IEnumerable
  • Compacting mathematical graph
  • Python argparse AssertionError when number of arguments exceeds threshold
  • AuthSub target path prefix does not match the provided “next” URL
  • ClickOnce is displaying the '.application' file as XML
  • Bluetooth Low Energy device scanning Failed with an exception
  • Play video in Vaadin
  • java 8 parallel stream Issue
  • Issue with terraform lookup
  • error: XML document must have a top level element
  • Capture image from webcam and display it - OpenCV - Eclipse - Windows
  • How to access WPF class library from Silverlight using iron python. Is it possible?
  • Show QMainwindow in the middle of the screen
  • Iterating through a folder using batch script
  • Encode string to Base64 in Inno Setup (Unicode Version of Inno Setup)
  • How do I set context and followup event in one intent?
  • Tableview make specific cell or row editable
  • Parallelization via JDBC - Pyspark - How does parallelization work using JDBC?
  • Special characters (ë) in JSON-response
  • Paging Through XML Data Using jQuery and HTML
  • Combine two jagged lists into one
  • Django REST framework - HyperlinkedRelatedField with additional parameter
  • Keep rows with certain values always at the bottom while sorting in jquery tablesorter plugin
  • How to use Typescript with libraries like Ampersand.js that parse configs to build prototypes
  • Comparing variables with strings bash
  • Drag and drop unicode TText in DelphiXe4
  • How to get rgb from transparent pixel in js
  • Why my AngularJS async test in Jasmine 1.3.x is not working?
  • Write to .csv file with PHP (Commas in Data Error)
  • What does the “id” field in an Android “Google Play Music” broadcast intent correspond to?
  • XSLT Transformation to validate rules in XML document