Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^2: Splitting a string on commas except when inside quotes

by Tux (Canon)
on Jan 02, 2023 at 17:11 UTC ( [id://11149294]=note: print w/replies, xml ) Need Help??


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

Though that is a perfect answer, the code you showed uses parse and thus is unsafe to use when upgrading from this easy single-line example to parsing a complete file record by record, wher getline whould be the preferred method to use.

Below is an example that uses more recent Text::CSV_XS' csv function

#!/usr/bin/perl use 5.014001; use warnings; use Text::CSV_XS qw( csv ); my $line = '11/21/2022,Payment,"Transfer to Smith, account 2",,USD,,123.60,'; say "IN: $line"; my $aoa = csv (in => \$line); my ($fDate, $fType, $fDetails, $fRef, $fCurrency, $fAmount, $fPaidOut, $fFees) = @{$aoa->[0]}; 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;

to upgrade from this single line to the complete file

my $aoa = csv (in => \$line);

changes to

my $aoa = csv (in => $file);

And then all is available in the $aoa

foreach my $record (@$aoa) { my ($date, $type, $dtls, $ref, $curr, $amt, $paid, $fees) = @$reco +rd; ...; }

Enjoy, Have FUN! H.Merijn

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11149294]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-26 07:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found