anadem has asked for the wisdom of the Perl Monks concerning the following question:
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 );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Splitting a string on commas except when inside quotes
by haukex (Archbishop) on Dec 27, 2022 at 07:12 UTC | |
|
Re: Splitting a string on commas except when inside quotes
by hippo (Archbishop) on Dec 27, 2022 at 10:47 UTC | |
by Tux (Canon) on Jan 02, 2023 at 17:11 UTC | |
|
Re: Splitting a string on commas except when inside quotes
by tybalt89 (Monsignor) on Dec 27, 2022 at 02:45 UTC | |
by Anonymous Monk on Jan 02, 2023 at 16:54 UTC | |
by tybalt89 (Monsignor) on Jan 02, 2023 at 20:38 UTC |