Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Perl ARRAY() result

by stevieb (Canon)
on Jan 13, 2019 at 23:19 UTC ( [id://1228499]=note: print w/replies, xml ) Need Help??


in reply to Perl ARRAY() result

There are a few responses to this post, but what I'd like to see, is a response with the full code body.

Can you do that guys?

I can run the code, even under strict/warnings, but I see sticky things, like the lack of the instantiation variable in the for loop for example. Typically, we don't use C-style for() loops in Perl code unless under duress, or weird circumstances. There are a couple of other smaller things, but I digress.

Anyone willing to post the full code that they have that got this to work, with the actual output from OPs example?

Replies are listed 'Best First'.
Re^2: Perl ARRAY() result
by kschwab (Vicar) on Jan 14, 2019 at 00:49 UTC
    Here's pme's changes as the whole script.
    use strict; use warnings; sub Solve { my ( $goal, $elements ) = @_; my ( @results, $RecursiveSolve, $nextValue ); $RecursiveSolve = sub { my ( $currentGoal, $included, $index ) = @_; for ( ; $index < @$elements ; ++$index ) { $nextValue = $elements->[$index]; if ( $currentGoal > 2 * $nextValue ) { $RecursiveSolve->( $currentGoal - $nextValue, [ @$included, $nextValue ], $index + 1 ); } else { if ( $currentGoal == $nextValue ) { #print join( ',', @$included, $nextValue ), "\n"; push @results, [ @$included, $nextValue ]; } return if $nextValue >= $currentGoal; } } }; $RecursiveSolve->( $goal, [], 0 ); undef $RecursiveSolve; # Avoid memory leak from circular refere +nce return @results; } my @results = Solve( 869, [ 15, 43, 51, 56, 60, 67, 122, 152, 193, 204, +229, 271, 293, 301 +] ); foreach my $result (@results) { print join(',',@$result),"\n"; }

      Absolutely brilliant!

      ++ and kudos for denoting a circular reference :)

        Oh, I didn't do anything here other than merge pme's changes with the original post and fix the formatting a little.
Re^2: Perl ARRAY() result
by BillKSmith (Monsignor) on Jan 14, 2019 at 14:56 UTC
    OK stevieb, here is the original OP code with only the print statement corrected as described in my first post. A sample execution demonstrates that the expected output is produced.
    >type pviki.pl use strict; use warnings; sub Solve { my ($goal, $elements) = @_; # For extra speed, you can remove this next line my (@results, $RecursiveSolve, $nextValue); $RecursiveSolve = sub { my ($currentGoal, $included, $index) = @_; for ( ; $index < @$elements; ++$index) { $nextValue = $elements->[$index]; # Since elements are sorted, there's no point in trying a # non-final element unless it's less than goal/2: if ($currentGoal > 2 * $nextValue) { $RecursiveSolve->($currentGoal - $nextValue, [ @$included, $nextValue ], $index + 1); } else { #print "@$_\n", [ @$included, $nextValue ] local $, = q(, ); print @$included, $nextValue, "\n" if $currentGoal == $nextValue; return if $nextValue >= $currentGoal; } } # end for }; # end $RecursiveSolve $RecursiveSolve->($goal, [], 0); undef $RecursiveSolve; # Avoid memory leak from circular reference return @results; } # end Solve my @results = Solve(869, [15, 43, 51, 56, 60, 67, 122, 152, 193, 204, +229, 271, 293, 301] ); >perl pviki.pl 15, 43, 51, 56, 60, 122, 229, 293, 15, 43, 51, 56, 204, 229, 271, 15, 43, 51, 67, 193, 229, 271, 15, 43, 51, 122, 152, 193, 293, 15, 56, 60, 67, 122, 152, 193, 204, 15, 56, 60, 122, 152, 193, 271, 15, 56, 204, 293, 301, 15, 67, 193, 293, 301, 43, 51, 56, 67, 122, 229, 301, 43, 51, 56, 67, 152, 229, 271, 43, 51, 60, 193, 229, 293, 43, 122, 204, 229, 271, 56, 60, 67, 122, 271, 293, 56, 67, 152, 293, 301,
    Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1228499]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-26 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found