Thread Probleme mit splice. Warum geht dat net???
(11 answers)
Opened by Alvin at 2008-01-12 17:22
Code beautified && in Perl-Tags gesetzt:
Keine weitere Aktion; Anmerkung am Ende. 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 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 #!/usr/bin/perl use strict; use warnings; #USAGE: ./alvin.pl 6 3 my $init_popsize = $ARGV[0] - 1; my $maxtime = $ARGV[1]; my @age; my @gender; my @weight; my @population = ( 0 .. $init_popsize ); for my $i ( 0 .. $#population ) { $age[$i] = int( rand(10) ); #Alter my $r1 = rand(); if ( $r1 <= 0.4 ) { #Geschlecht (0=männlich, 1=weiblich) $gender[$i] = 0; } else { $gender[$i] = 1; } my $r2 = rand(); if ( $r2 <= 0.3 ) { #Gewicht $weight[$i] = 0; } elsif ( $r2 > 0.3 && $r2 <= 0.6 ) { $weight[$i] = 1; } else { $weight[$i] = 2; } printf "$age[$i] $gender[$i] $weight[$i]\n"; } printf "\n"; Aussen: for my $t ( 1 .. $maxtime ) { Innen: for my $i ( 0 .. $#population ) { $age[$i]++; if ( $age[$i] >= 10 ) { splice( @age, $i, 1 ); splice( @weight, $i, 1 ); splice( @gender, $i, 1 ); splice( @population, $i, 1 ); # $age[$i]++; next Innen; } } for my $i ( 0 .. $#population ) { printf "$age[$i] $gender[$i] $weight[$i]\n"; } my $population = $#population + 1; printf "\npopulation: $population\n"; } Anderes Thema als das gefragte: Du benutzt printf. Warum? Wenn Du keine Formatierung der Ausgabe machst, dann nimm print; wenn Du was formatieren willst, dann printf. Denn ansonsten ist das eher eine Kanone für den Spatz. 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! |