I’m trying to read elements from an array using a for loop, but I can’t seem to get it working right. When I run the program, it prints out a weird “HASH”, or doesn’t print out anything. Can anyone help?
#!/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";
}
When you put
{}, you explicitly askperlto make a reference to aHASH. What you seems to need instead is using parenthesis to declare anARRAY.So :
Moreover, making arrays is more fun and less boring like that :
but it only works when you don’t have any space for values.
NOTES
use warnings;all the times@selections[$he]but$selections[$he]$hein the whole scope, see where I declare itHASHinstead of twoARRAYS:like this :