Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



You couldn't get it to work, but it certainly does work: https://chat.openai.com/share/774fbe29-00b9-4e1f-815c-268707...


This 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.


I wonder if inputting an explicit function call

    $csv = str_getcsv(file($argv[1]))
Would make it easier.



One shot code translation is very… September 2023


Hmm but what's the solution? Type out the reasoning for every line of code for an entire codebase?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: