10
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Perlライブラリ無し縛り - 引用符内改行ありのCSVパース

Last updated at Posted at 2014-05-14
  • ダブルクォーテーションに囲まれた文字列中の改行、カンマを区切り文字として扱わない
  • ライブラリは使わない。インタプリタの機能のみを使う
&csvparse( sub{ <DATA> } , sub{ print map("[$_]",@{$_[0]}),"\n"; } );

sub csvparse{
    my ($read,$callback)=@_;
    while( defined(my $line=$read->()) ){
        for(;;){
            $line =~ s/"([^"]+)"/"\a".unpack('h*',$1)."\a"/ge;
            last unless $line =~ /"/ && defined(my $next = $read->());
            $line .= $next;
        }
        chomp $line;
        my @csv = split(/,/,$line);
        s/\a([^\a]+)\a/pack('h*',$1)/ge foreach( @csv );
        $callback->( \@csv );
    }
}
__END__
nihon,go ahaha,ihihi
ahaha,"ihihi , ufufu","ohoho
mumumu
ahaha
gahaha"
XXXXX,"YYYYY","DDDDD,XXXXXX"
  • CSV解析関数 csvparse の
    • 第一引数は、1行を取得するクロージャを渡す。
    • 第二引数は、分解した CSV を引き渡すクロージャーを渡す。
$ perl csv.pl
[nihon][go ahaha][ihihi]
[ahaha][ihihi , ufufu][ohoho
mumumu
ahaha
gahaha]
[XXXXX][YYYYY][DDDDD,XXXXXX]

初出:MHI 5.0 - [Perl] ライブラリ無し縛り - CSVパース

10
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?