Weinachtsbaum

Lösung

#!/usr/bin/perl
# Schreibt das Bild eines Weihnachtsbaums auf die Konsole
# auf gewünchte Höhe

use strict;

my $blank=" ";
my $star="*";
my $forwardslash="/";
my $backslash="\\";
my $dot=".";
my $comma=",";
my $roof="^";
my $bracketLeft="[";
my $bracketRight="]";
my $underscore="_";
my $newline="\n";
my $lines;
my $width;

print "Bitte geben Sie die Höhe des Baumes ein (4-...): ";
chomp($lines = <>);
$width = ($lines/2);

for(my $l=0;$l<$lines-1;$l++) {
  #spaces at the beginning of each line
  for(my $w=0;$w<$width;$w++) {
    print $blank;
  }
  #for first line only
  if($l==0) { 
    print $star; 
    $width--;
  }
  else {
    #for even line numbers
    if($l%2==0) {
      print $forwardslash;
        for(my $i=0;$i<$l-1;$i++) {
          if($i%2==0) {
            print $comma;
          } else { print $dot;}
        }
      print $backslash;
      $width--;
    }
    #for odd line numbers
    elsif($l%2!=0) {
        print $forwardslash;
        for(my $i=0;$i<$l;$i++) {
          if($i%2==0) {
            print $dot;
          } else { print $comma;}
        }
      print $backslash;
    }
  }
  print $newline;
}
  
for(my $i=0;$i<$lines/2-1;$i++) {
  print $roof;
}
print $bracketLeft;
print $underscore;
print $bracketRight;
for(my $i=0;$i<($lines/2)-1;$i++) {
  print $roof;
}
print $newline;