Perl Monk, Perl Meditation PerlMonks

### comment on

 Need Help??

A more Perlish cleaner version might look like:

```use strict;
use warnings;

my @row = map {chomp; [split '']} <DATA>;
my \$visible;

die "Data too awful to contemplate\n" if @row < 2 || @{\$row[0]} < 2;

for my \$col (1 .. \$#{\$row[0]} - 1) {
my \$tHigh = \$row[0][\$col];
my \$bHigh = \$row[-1][\$col];

\$visible += 2; # Edge trees always visible

for my \$depth (1 .. \$#row - 1) {
test(\\$tHigh, \\$row[\$depth][\$col]) if \$tHigh != 9;
test(\\$bHigh, \\$row[-\$depth - 1][\$col]) if \$bHigh != 9;
last if \$tHigh == 9 && \$bHigh == 9;
}
}

for my \$rowIdx (1 .. \$#row - 1) {
# Count down from top edge
my \$lHigh = \$row[\$rowIdx][0];
my \$rHigh = \$row[\$rowIdx][-1];

\$visible += 2; # Edge trees always visible

for my \$depth (1 .. \$#row - 1) {
test(\\$lHigh, \\$row[\$rowIdx][\$depth]) if \$lHigh != 9;
test(\\$rHigh, \\$row[\$rowIdx][-\$depth - 1]) if \$rHigh != 9;
last if \$lHigh == 9 && \$rHigh == 9;
}
}

\$visible += 4; # We didn't consider the corners so do that now.
print \$visible;

sub test {
my (\$highest, \$cell) = @_;

return if \$\$highest >= \$\$cell;

if (\$\$cell !~ /\./) {
++\$visible;
\$\$cell .= '.';
}

\$\$highest = \$\$cell;
return \$\$highest == 9;
}

__DATA__
30373
25512
65332
33549
35390

which outputs 21 - the value suggested by the challenge. Note that is only deals correctly with rectangular forests containing 4 or more trees.

Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

In reply to Re: Uninitialized warnings trouble by GrandFather
in thread Uninitialized warnings trouble by Anonymous Monk

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.
• Posts may use any of the Perl Monks Approved HTML tags:
a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, 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, 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?

Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-09-16 06:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
The PerlMonks site front end has:

Results (21 votes). Check out past polls.

Notices?
 • erzuuli ‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.