To print a Hollow Diamond Pattern in Perl, we use loops and conditional statements to manage spaces and stars.
In this example,
# Print Hollow Diamond Pattern in Perl
use strict;
use warnings;
my $num_rows = 5;
foreach my $row (1..$num_rows) {
my $pattern = ' ' x ($num_rows - $row);
if ($row == 1) {
$pattern .= '*';
} else {
$pattern .= '*' . ' ' x (2 * $row - 3) . '*';
}
print $pattern . "\n";
}
foreach my $row (reverse(1..($num_rows - 1))) {
my $pattern = ' ' x ($num_rows - $row);
if ($row == 1 || $row == $num_rows) {
$pattern .= '*';
} else {
$pattern .= '*' . ' ' x (2 * $row - 3) . '*';
}
print $pattern . "\n";
}
* * * * * * * * * * * * * * * *
In this tutorial, we learned How to print Hollow Diamond Pattern in Perl language with well detailed examples.