austenpma / csvfix

Automatically exported from code.google.com/p/csvfix
MIT License
0 stars 0 forks source link

The if command should use lazy evaluation #57

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?

1. Create a file test.csv with the following content:
   A,B
   0,1
2. Run csvfix eval -e 'if(isnum($2),$2+1,$2)' test.csv

What is the expected output? What do you see instead?

The expected result is:
"A","B","B"
"0","1","2"

What I got:
ERROR: Invalid numeric value B

What version of the product are you using? On what operating system?
1.2, Linux

Please provide any additional information below.
if should evaluate its arguments lazily. Here the problem comes from the fact 
(I guess) that both $2 and $2+1 are evaluated regardless of whether the 
condition evaluates to true or false.

There are two other problems with eval:
1. Why is everything in quotation marks in the output?
2. In case of a csv file with headers, there should be a switch which tells 
eval to disregard the first line, but copy it to the output nevertheless. -ifn 
consumes the first line, so it's not applicable in this case.

Original issue reported on code.google.com by nemesk...@gmail.com on 21 Jun 2012 at 3:29

GoogleCodeExporter commented 8 years ago
I must admit, I thought it did do short circuited evaluation, but you are 
right! I will look at it. Regarding the quotes, use -smq to do smart-quoting, 
which will only quote when absolutely necessary. The header issue is a 
recurring problem - I will have another look at it.

BTW, in future, please post ONE issue at a time here - if you want to raise 
multiple issues, and ask questions about usage, please use the support group at 
http://groups.google.com/group/csvfix, where you will typically get  a faster 
response than posting here.

Original comment by nbutterworth1953@gmail.com on 22 Jun 2012 at 8:52

GoogleCodeExporter commented 8 years ago

Original comment by nbutterworth1953@gmail.com on 26 Jun 2012 at 7:55

GoogleCodeExporter commented 8 years ago
Unfortunately, the expression evaluation engine used by CSVfix is resolutely  
going to treat if() as a function. As in most languages, ALL function arguments 
are evaluated before the function is called, so it is not possible (for 
function calls) to do short-circuited evaluation. I mistakenly (god knows why) 
thought I had implemented SCE, so this is  a documentation issue for the 
current major version, which I will correct in the next release.

I recognise that short-circuited evaluation is extremely desirable, and will 
hopefully implement it in CSVfix 2.0, for which do not hold your breath.

I am therefore reluctantly marking this as WontFix.

Original comment by nbutterworth1953@gmail.com on 24 Jul 2012 at 6:58