To print a hollow pyramid pattern in Perl, you can use loops. The outer loop controls the rows, and the inner loops manage the spaces and stars to create the hollow effect.
In this example,
#!/usr/bin/perl
use strict;
use warnings;
sub print_hollow_pyramid {
my $rows = shift;
for my $i (1..$rows) {
my $pattern = ' ' x (($rows - $i) * 2);
for my $j (1..(2 * $i - 1)) {
if ($j == 1 || $j == (2 * $i - 1) || $i == $rows) {
$pattern .= '* ';
} else {
$pattern .= ' ';
}
}
print "$pattern\n";
}
}
print_hollow_pyramid(5);
* * * * * * * * * * * * * * * *
In this tutorial, we learned How to print Hollow Pyramid Pattern in Perl language with well detailed examples.