I have an array of hash references:
my @price = (
{
id => '1',
label => 5.00
},
{
id => '2',
label => 7.50
},
);
I also have 2 variable integers, $diff and $last. when I try to iterate over the array to multiply the “label” by $diff/$last:
foreach (@price) {
print "(($diff/$last)*$_->{label})\t";
}
i get the following output:
((12/30)*5.00)
((12/30)*7.50)
I assume it is not multiplying because I have two integers and a hash reference. If this is correct, how can I make the hash reference an integer? How can I force Perl to actually do the math and print the resulting product?
It’s not multiplying because all of that is inside the double quotes. You need to do:
The way you’re doing it perl is simply interpolating the variables and printing their values because they are enclosed within the quotes. The
*is simply treated as a character to print. The above does the math then the.causes a concatenation to"\t"(which converts the result to a string).Outputs: