www.farid-hajji.net banner

Farid Hajji

Perl: Einführung, Anwendungen, Referenz (2/e) [Support-Site]

Farid Hajji: Perl - Einführung, Anwendungen, Referenz
2., aktualisierte und erweiterte Auflage
Addison-Wesley Longman, ISBN 3-8273-1535-2

Beispielprogramm

closure3.pl
#!/usr/local/bin/perl -w
# closure3.pl -- Closures im Streaming-Modus: Kartesisches Produkt

sub make_cartesian {
    my $list1 = shift;        # Zeiger auf die erste Liste
    my $list2 = shift;        # Zeiger auf die zweite Liste
    my ($i, $j) = (0, 0);     # Aktuelle Zeigerposition
    return sub {
    ($j = 0, ++$i) if $j >= @$list2;
    ($i = 0, return ()) if $i >= @$list1;

    return ($list1->[$i], $list2->[$j++]);
    }
}

@colors = qw (red yellow green);
@directions = qw (straight left right);

$crossing = make_cartesian(\@colors, \@directions);
$chess    = make_cartesian([1 .. 8], ['a' .. 'h']);

# Welche Kombinationen sind hier richtig?
while (($col,$dir) = &$crossing()) {
    print "$col and $dir? "; chomp($answer = <STDIN>);
    push(@answers, $answer);
}

# Dazwischen Schachbrett-Kombinationen ausgeben:
while (($col,$row) = &$chess()) { print $col, $row, " "; }
print "\n";


# Noch einmal iterieren:
print "Your answers:\n";
while (($col,$dir) = &$crossing()) {
    print "($col, $dir) => ", shift(@answers), "\n";
}
   

[Prev] [Up] [Relevant Chapter] [Next]

[Alte Quelle]


Last modified: $Date: 2006/05/18 12:55:54 $
FH. Search :: Sitemap :: Disclaimer :: Copyright :: Privacy
FreeBSD Logo