I’m trying to format my output to look like it’s in columns. I’m trying to use the printf function.
Here’s what I have:
printf("%s %10s %12s %10s\n", "Qty", "Desc.", "Unit \$", "Total");
for ($he = 0; $he <= 6; $he++) {
if (@quantity[$he] != 0) {
printf("%d %10s %12.2f %10.2f\n", @quantity[$he], @selections[$he], @prices[$he], @prices[$he] * @quantity[$he])
}
}
I’m trying to make it so that the second printf inside of the if statement of the for loop lines up with the “Qty”, “Desc.”, “Unit \$” and “Total.”
You need to use the same numbers in the two formats:
and
Note that 12.2 means (12 digits + 1 point + 2 digits), which is why I wrote 15 in the first format. The same goes for the 13.
Also note that you’re accessing array elements incorrectly.
Instead of
@quantity[$he]use$quantity[$he]. That is, replace the@with a$.