2
我正在嘗試使用for循環讀取數組中的元素,但似乎無法使其正確工作。當我運行程序時,它會打印出一個奇怪的「HASH」,或者不打印任何東西。誰能幫忙?嘗試使用for循環讀取數組中的元素
#!/usr/bin/perl
use strict;
my $he;
my @selections = {"Hamburger","Frankfurter","French Fries","Large Coke","Medium Coke","Small Coke","Onion Rings"};
my @prices = {3.49, 2.19, 1.69, 1.79, 1.59, 1.39, 1.19};
for($he= 0; $he<= 6; $he++)
{
print "@selections[$he]";
print "@prices[$he]\n";
}
不宜硬編碼 「6」。 'for(my $ he = 0; $ he <= 6; $ he ++)'應該爲'(my $ he = 0; $ he <@selections; $ he ++)',並且它的可讀性更高,爲'for我$他(0 .. $#選擇)' – ikegami