以前我總是給出具體的例子,但這次給你們出一道足以使你們頭發變白,身心疲憊的難題: #!/usr/bin/perl # A Political evaluation script
$a=“A1” $b=“George”; if($a>$b)print $a wordinake a better Presideut
If($a<$b) print $b word make a betler Presiclent ; If($a=$b)Print $a or $b, tlere’s no Diflereue ; 輸出結果表明其中并沒有什么不同,這也許只反映出一個政治性的現實,但我們比較出的結果又是什么呢?對了,我們應該使用字符串操作,不對嗎? #!/usr/bin/perl # A political evaluation script
$a = "Al"; $b = "George";
if ( $a > $b) { print "$a would make a better President. "; } if ( $a < $b) { print "$b would make a better President. "; } if ( $a==$b) { print "$a or $b, there's no difference... "; }
呵呵,輸出表明它們倆沒有什么不同的地方。這也許是政治上的真實反映,但對我們期望的比較輸出卻……哦,對了,我們應該使用字符串操作符,恩?
#!/usr/bin/perl # A political evaluation script
$a = "Al"; $b = "George";
if ( $a gt $b) { print "$a would make a better President. "; } if ( $a lt $b) { print "$b would make a better President. "; } if ( $a eq $b) { print "$a or $b, there's no difference... "; }
現在,比較操作符才能正確的工作。(現實世界的邏輯卻并非如此……我離題了。) 順便提一下,在第一個例子中為什么Perl會認為“Al”和“George”是等值的呢?什么時候程序中參進了政治觀點? 很重要的一點原因是,它要依賴Perl分辨“真(true)”與“假(false)”的方法。可以進行一些試驗,如“if”,“while”,“wntil”等,根據實驗的結果,我們可以理解這一點。
|