dideod.yang has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks. I have to convert some script from perl to tcl.. But it didn't work well.. Can you help me? I don't know why print/puts are working well, but return didn't work.
## PERL ########## use warnings; use strict; my @input = qw/ 2 3 5 /; my $target = 10; my @test = &find( 0, [], [], $target, @input ); foreach my $test (@test) { print "@{$test}\n"; } sub find { my ($sum, $sofar, $output, $want, @numbers ) = @_; # print "$sum //// @{$sofar} /// @numbers\n"; if( $sum == $want ) { # print "@{$sofar}\n"; push @{$output},$sofar; } elsif( $sum < $want and @numbers and $numbers[0] > 0 ) { find( $sum + $numbers[0], [ @{$sofar}, $numbers[0] ], $output, $wa +nt, @numbers ); find( $sum, $sofar, $output, $want, @numbers[1..$#numbers] ); } return @{$output}; } ############# TCL proc combinationSum {sum sofar want numbers output } { if { $sum == $want } { #puts $sofar lappend output $sofar } if { ( $sum < $want ) && ( [lindex $numbers 0] > 0 ) && ( [llength + $numbers] > 0 ) } { combinationSum [expr $sum + [lindex $numbers 0]] [concat $sofa +r [lindex $numbers 0]] $want $numbers $output combinationSum $sum $sofar $want [lrange $numbers 1 end] $outp +ut + } return $output } set test_input [list 2 3 5 ] set test_target 10 set test_output [combinationSum 0 [] $test_target $test_input []]

Replies are listed 'Best First'.
Re: convert script from perl to tcl
by hippo (Archbishop) on Aug 07, 2019 at 08:40 UTC
    I have to convert some script from perl to tcl.

    Why? You already have a perl script which (supposedly) works. Why translate it into a more niche scripting language which you understand less than Perl?

      Educated guesses?
Re: convert script from perl to tcl (brain....)
by Anonymous Monk on Aug 07, 2019 at 07:58 UTC
Re: convert script from perl to tcl
by LanX (Saint) on Aug 07, 2019 at 06:04 UTC
    Off topic, we are no TCL experts here.

    My guess (!) is at least your use of sigils is wrong.

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Re: convert script from perl to tcl
by atcroft (Abbot) on Aug 08, 2019 at 03:55 UTC

    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:

    Verification:

    (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.