5 ms·
It doesn't work. I asked GPT-4 to migrate the following PHP code to JS: $csv = array_map('str_getcsv', file($argv[1])); And this is the result: const
by WA 3y ago
It doesn't work. I asked GPT-4 to migrate the following PHP code to JS:
$csv = array_map('str_getcsv', file($argv[1]));
And this is the result:
const csv = readFileSync(filename, 'utf-8').split('\n').map(line => line.split(','));
You can't parse CSV this way, because you need to respect delimiters. Counter example:
1,"1,5",2
"1,5" being the German notation for "1.5". Hence, a simple split(',') will break this thing.
PHP's str_getcsv is, of course, a proper CSV parser and not a string splitter. Unless your code uses basically zero stdlib API calls, you will have to double check everything.
Please note that this kind of bug isn't even easy to catch if you test CSV file doesn't contain a quoted entry.
- quickthrower2 3y agoOne shot code translation is very… September 2023
- WA 3y agoHmm but what's the solution? Type out the reasoning for every line of code for an entire codebase?
- mejutoco 3y agoI wonder if inputting an explicit function call $csv = str_getcsv(file($argv[1])) Would make it easier.
- jiggawatts 3y agoYes: https://chat.openai.com/share/33466769-1e06-49af-8abd-7a4c0e691254 https://chat.openai.com/share/33466769-1e06-49af-8abd-7a4c0e...
- BoorishBears 3y agoYou couldn't get it to work, but it certainly does work: https://chat.openai.com/share/774fbe29-00b9-4e1f-815c-268707283153 https://chat.openai.com/share/774fbe29-00b9-4e1f-815c-268707...
- AltruisticGapHN 3y agoThis is very cool. Interestingly GPT appears to be incorrect when it suggests in the differences, that str_getcsv would not parse correctly quoted parts. It does look like the php function has support for the "enclosure" character hence something like "1,5" should parse correctly.