#!/usr/bin/perl #shortest_longest.pl FILE use warnings; use strict; my($n,$w,@words,$words,$longest,$shortest,@longest_list,@shortest_list,@sorted_short,@sorted_long,$array_long,$array_short,@lines,$x,@longest_line,@shortest_line,$y); while (<>) { chop; tr/;:.,!\-'"'?//d; tr/A-Z/a-z/; foreach $w (split) { push(@words, $w); push(@lines, "$.\t ($_)"); } } $longest = ""; $shortest = $words[0]; $n = 0; while ($words[$n]) { if (length($words[$n]) >= length($longest)) { $longest = $words[$n]; } elsif (length($words[$n]) <= length($shortest)) { $shortest = $words[$n]; } $n++; } $n = 0; while ($words[$n]) { if (length($words[$n]) == length($longest)) { push(@longest_list,$words[$n]); push(@longest_line,$lines[$n]); } elsif (length($words[$n]) == length($shortest)) { push(@shortest_list,$words[$n]); push(@shortest_line,$lines[$n]); } $n++; } @sorted_short = sort(@shortest_list); @sorted_long = sort(@longest_list); $array_long = @longest_list; $array_short = @shortest_list; $n = 0; if ($array_long > 1) { print "Die laengsten Woerter mit " . length($longest) . " Buchstaben sind: \n@sorted_long\n\n"; } else { print "Das laengste Wort ist $longest.\n\n"; } while ($longest_list[$n]) { foreach $x (@longest_line) { print "$longest_list[$n] in der Zeile $x.\n"; $n++; } } if ($array_short > 1) { print "\nMit " . length($shortest) . " Buchstaben die kuerzesten Woerter sind: \n@sorted_short\n\n"; } else { print "Das kuerzeste Wort ist $shortest\n"; } $n = 0; while ($shortest_list[$n]) { foreach $y (@shortest_line) { print "$shortest_list[$n] in der Zeile $y.\n"; $n++; } }