Unfortunately the provider of the data has newly added a comma in the fDetails field and delimited the field by double-quotes:( $fDate, $fType, $fDetails, $fRef, $fCurrency, $fAmount, $fPaidOut, $fFees, $fTax ) = split( /,/, $line );
11/21/2022,Payment,"Transfer to Smith, account 2",,USD,,123.60,,,
where previously there was neither a comma following Smith nor quotes around the details; split now erroneously gives:
$fDetails = "Transfer to Smith $fRef = Savings account"
with subsequent fields also getting the wrong data of course.
What's a clean way to handle this, such that the comma within the double-quotes is ignored by split? (I don't mind losing the comma within the details if that's easiest.) Kindly excuse my ignorance here, I retired some years ago and my brain has rusted.Here's a runnable fragment:
$line = '11/21/2022,Payment,"Transfer to Smith, account 2",,USD,,123.60,'; print " $line\n"; ( $fDate, $fType, $fDetails, $fRef, $fCurrency, $fAmount, $fPaidOut, $fFees ) = split( /,/, $line ); printf("\n Date: %s; Type: %s; \n" ." Details: %s\n Reference: %s\n" ." Currency: %s; Amount: %s; PaidOut: %s; Fees: %s;\n", $fDate, $fType, $fDetails, $fRef, $fCurrency, $fAmount, $fPaidOut, $fFees );
In reply to Splitting a string on commas except when inside quotes by anadem
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |