#!/usr/bin/perl use warnings; use strict; use Text::CSV_XS; # Why not get the full strenth of XS? my $codepage = 'utf8'; binmode(STDOUT, ":$codepage"); binmode(STDIN, ":$codepage"); # STDOIN? #get file names: my $fileTable = shift; my @fldNames = qw( id fio snils no0 data vyplata adres_bank0 no1 no2 n +o3 adres_bank1 no4 no5 blank no6 ); my %fldNum; # Better name @fldNum{@fldNames} = 0 .. $#fldNames; # Shorter, faster. my @tblRows; my $CSV_H = Text::CSV_XS->new({ # Missing "my". sep_char => ';', binary => 1, blank_is_undef => 1, empty_is_undef => 1, allow_whitespace => 0, allow_loose_quotes => 1, auto_diag => 1 }); $CSV_H->callbacks(error => \&csverror2023); my $fixed; sub csverror2023{ print join('/', @_), "\n"; my $charpos = $_[2]; my $srcstr = $CSV_H->error_input(); printf "ERROR_INPUT: %s\n", $srcstr; unless($CSV_H->eof){ my $badchar = quotemeta(substr($srcstr, $charpos - 1, 1)); printf "BadChar: (%s)\n", $badchar; my $prestr = substr($srcstr, 0, $charpos - 1); printf "PreMatch: (%s)\n", $prestr; $prestr =~ /$badchar[^$badchar]*$/; printf "MATCH_iNDEXES: (%s-%s)\n", $-[0], $+[0]; printf "BadChar0: (%s)\n", substr($prestr, $-[0], 1); substr($srcstr, $-[0], 0, '\\'); printf "ModifiedString: (%s)\n", $srcstr; $fixed = $srcstr; } $CSV_H->SetDiag(0); } if(open my $TBL_H, "<:encoding($codepage)", $fileTable){ # No need to + double quote a single variable. until($CSV_H->eof){ undef $fixed; my $row = $CSV_H->getline($TBL_H); while ($fixed) { my $line = $fixed; undef $fixed; $CSV_H->parse($line); $row = [$CSV_H->fields]; } unless($row->[$fldNum{id}] !~ /^[[:digit:]]+$/ || $row->[$fldN +um{fio}] =~ /^[[:digit:]]+$/){ printf "%12s: %s\n", $fldNames[$_], $row->[$_] for 0 .. $# +{$row}; push @tblRows, $row; } print '=' x 100, "\n"; } close $TBL_H; }
In reply to Re: pass repaired csv string from error handler callback function back to the csv's getline loop for reparsing
by choroba
in thread pass repaired csv string from error handler callback function back to the csv's getline loop for reparsing
by igoryonya
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |