Thread (Verständnis)Probleme mit verschachtelten foreach-Schleifen und If-Abfragen (11 answers)
Opened by Wurzel at 2008-08-07 19:28

Linuxer
 2008-08-08 20:36
#113256 #113256
User since
2006-01-27
3891 Artikel
HausmeisterIn

user image
@Wurzel:
Du kannst auch die zwei greps innerhalb einer if-Abfrage prüfen:
(Ausserdem das zweifelhafte $/ entfernt)

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/usr/bin/perl -w
use strict;

my @konsonants = qw( p b t d k g m n f v h ch l r s sch ts ds tsch dsch x ); #21
my @not_first  = qw( p b t d k g ); #6
my @not_second = qw( p b t d k g m n ts ds tsch dsch x ); #13

my $j = 0;
{
    local $\ = "\n"; # $/ vermieden; wer weiss, was da drin steckt ;o))

    foreach my $konso1 (@konsonants) {
        foreach my $konso2 (@konsonants) {
            next if ( $konso1 eq $konso2 );
            next if ( grep { $konso1 eq $_ } @not_first and grep { $konso2 eq $_ } @not_second );
            print $konso1, $konso2; # $\ wird angehangen
            $j++;
        }
    }

    print $j; # $\ wird angehangen
}

[/quote]
meine Beiträge: I.d.R. alle Angaben ohne Gewähr und auf Linux abgestimmt!
Die Sprache heisst Perl, nicht PERL. - Bitte Crossposts als solche kenntlich machen!

View full thread (Verständnis)Probleme mit verschachtelten foreach-Schleifen und If-Abfragen