As far as I can tell, I'm not using any array references, yet I get a runtime error of
Can't use an undefined value as an ARRAY reference at make_change.pl l
+ine 12.
when I run this code:
use strict;
use warnings;
use re 'eval';
my @denoms = (10, 6, 5, 1);
my $expr = join '', map "((?:$_)*)", map '1' x $_, @denoms;
print "E=$expr\n";
sub greedy_change {
my $change = shift;
my $coin_count = 0;
my @combo;
$_ = ('1' x $change);
print "Trying to change $change with @denoms...\n";
print "$expr on $_\n";
if (
m{^$expr$
(?{
print "Code doesn't get run after once.\n";
my @lengths = ($+[1]);
my $new_count = 0;
$lengths[$_] = $+[$_+1] - $+[$_] for 1..$#denoms;
$new_count += $lengths[$_]/$denoms[$_] for 0..$#denoms;
if (!$coin_count or $new_count < $coin_count) {
print "$change: $new_count is better than $coin_count\n";
$coin_count = $new_count;
@combo = @lengths;
}
})
}x) {
print "Matched!\n";
}
if ($coin_count) {
print "C=$coin_count\n";
}
else {
print "No matching combination found\n";
}
@combo;
}
for my $num (12, 14, 36) {
my @change = greedy_change($num);
print "$num: @change\n";
}
I know I'm doing some weird stuff, but the errors I've been getting just make no sense. Now my eyes are fried, and I need some fresh ones to help me out.
Caution: Contents may have been coded under pressure.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.