Ho il seguente script:
#!/usr/bin/perl
use warnings;
use strict;
my $count = 0;
my (@first , @second , @third);
while ($count <= 7){
push (@first , $count);
push (@second , $count) if defined $count;
push (@third , $count) if $count;
$count++;
}
print "first: @first\n";
print "second: @second\n";
print "third: @third\n";
Questo produce il seguente output:
first: 0 1 2 3 4 5 6 7
second: 0 1 2 3 4 5 6 7
third: 1 2 3 4 5 6 7
Qual è la differenza tra mettere if defined $count
vs if $count
, oltre il secondo metodo non aggiungerà lo zero a l'array? Ho cercato i perldocs ma non ho trovato la risposta.
deve essere: "quando $ count valuta! = 0" –
@nathan. Saluti –
Entrambi funzionano allo stesso modo, in realtà. È il valore che mettono alla prova che è diverso. Il primo verifica il valore di $ count, il secondo il valore di $ count definito. se() non importa come si ottiene il valore. –