The problem I saw (from printing the values just after proc entry) was that $output was not being updated. Since I've only had to use TCL a few times (and consider myself still a beginner at it), I just did something that worked-I removed the output variable from the parameter list and used 'upvar #0 output outp' to access it as a global variable.

WARNING: If this is some form of homework, the use of a global variable may result in a low or failing grade.

TCL Code:

#!/usr/bin/tclsh # vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: set output [ ] proc combinationSum { sum sofar want numbers } { upvar #0 output outp # puts "\t$sum | $sofar | $want | $numbers | $output" if { $sum == $want } { lappend outp $sofar return TRUE } else { if { ( $sum < $want ) && ( [ lindex $numbers 0 ] > 0 ) && ( [ llength $numbers ] > 0 ) } { combinationSum \ [ expr $sum + [ lindex $numbers 0 ] ] \ [ concat $sofar [ lindex $numbers 0 ] ] \ $want $numbers combinationSum $sum $sofar $want [ lrange $numbers 1 end ] } return TRUE } } set test_input [ list 2 3 5 ] set test_target 20 set test_output [ combinationSum 0 [] $test_target $test_input ] # puts "> $output" # puts [ llength $output ] foreach l $output { puts $l }

Verification:

Output from OP-provided perl script (target: 20; input: [ 2, 3, 5, ]):

$ ./11104071-perl.pl 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 2 2 2 2 2 2 3 5 2 2 2 2 2 5 5 2 2 2 2 3 3 3 3 2 2 2 3 3 3 5 2 2 3 3 5 5 2 3 3 3 3 3 3 2 3 5 5 5 3 3 3 3 3 5 5 5 5 5

Output from above TCL script (target: 20; input: [ 2, 3, 5, ]):

$ ./11104071-tcl.tcl 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 2 2 2 2 2 2 3 5 2 2 2 2 2 5 5 2 2 2 2 3 3 3 3 2 2 2 3 3 3 5 2 2 3 3 5 5 2 3 3 3 3 3 3 2 3 5 5 5 3 3 3 3 3 5 5 5 5 5

(As an aside, the lack of formatting made both pieces of code hard to follow (although other things may have my focus off tonight as well). I ran the perl code through Perltidy, and the TCL code through the code listed as "reformat2.tcl" here, but I still had to run it with different values to determine what your actual goal was (all combinations of @input that sum to $target).)

Hope that helps.


In reply to Re: convert script from perl to tcl by atcroft
in thread convert script from perl to tcl by dideod.yang

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.