Why put a semicolon after the last input for an array?

Illustration
David - 2023-11-07T11:26:11+00:00
Question: Why put a semicolon after the last input for an array?

This is an example from a textbook that I'm working with as a beginner. Why would this have semi colons after the 8.99 and -1.50?     updatePriceTable([19.99, 9.99; 14.99, 8.99;], [-1.00; -1.50;], 1)  

Expert Answer

Profile picture of Kshitij Singh Kshitij Singh answered . 2025-11-20

Suppose that you are writing out several rows of value using code. You write out the initial [ and you start writing values. You reach the end of the first row. You now have three possible strategies:
  1. Test to see if you are at the last row, and if not then put in ; -- and after you leave the loop, put in the ] and continue with whatever you were doing; OR
  2. write out ; anyhow -- and after you leave the loop, put in the ] and continue on with whatever you were doing; OR
  3. create all of the row-by-row strings in memory, and after you are doing, strjoin() them with ; (which will not put a semi-colon at the end); then put on the ] and write it all out to output, and then continue on
The first strategy requires a last-row test every iteration, which is difficult to vectorize unless you use hacks such as having an output variable that contains semi-colon for all rows except the last but is empty for the last one.
 
The third strategy requires that the output fits into memory
 
The second strategy can be vectorized. It's "cost" is that you end up with one extra semi-colon on output that is not strictly needed.


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!