in reply to Splitting a string on commas except when inside quotes

Like this?

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11149097 use warnings; my $line = '11/21/2022,Payment,"Transfer to Smith, account 2",,USD,,123.60,'; print " $line\n"; my ( $fDate, $fType, $fDetails, $fRef, $fCurrency, $fAmount, $fPaidOut, $fFees ) = "$line," =~ /(".*?"|[^,]*),/g; # NOTE # = 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 );

Outputs:

11/21/2022,Payment,"Transfer to Smith, account 2",,USD,,123.60, Date: 11/21/2022; Type: Payment; Details: "Transfer to Smith, account 2" Reference: Currency: USD; Amount: ; PaidOut: 123.60; Fees: ;

Replies are listed 'Best First'.
Re^2: Splitting a string on commas except when inside quotes
by Anonymous Monk on Jan 02, 2023 at 16:54 UTC
    my $line = '11/21/2022,Payment,"Transfer to Smith, account 2",,USD,,123.60,'; print " $line\n"; my ( $fDate, $fType, $fDetails, $fRef, $fCurrency, $fAmount, $fPaidOut, $fFees ) = "$line," =~ /(".*?"|[^,]*),/g; # NOTE
    What is that comma and double quotes doing to $line?

      The regex looks for a trailing comma for every field. "$line," just adds one for the last field.