csvãã¡ã¤ã«ãèªã¿è¾¼ãã§Perlã®ããã·ã¥ã®é åã«å¤æãã
Perl
›
ãã¡ã¤ã«å ¥åºå
›
here
ãcsvå½¢å¼ã®ãã¡ã¤ã«ãèªã¿è¾¼ãã§ãããã·ã¥ã®é åã«å¤æãããµã³ãã«ã§ãã
ãããã·ã¥ã¹ã©ã¤ã¹ã¨ããæ©è½ãç©æ¥µçã«ä½¿ã£ã¦ããã®ã§ãå¯èªæ§ã¯ã¡ãã£ã¨ä½ãã§ããPerlããã°ãã使ã£ã¦æ £ãã¦ããªãã¨æå·ã«è¦ããããããã¾ãããååã®ãµã³ãã«ã¨ã»ã¼æ§é ã¯åããªã®ã§ãéãé¨åã ãã解説ãã¾ãã
use strict; use warnings; # å¼æ°ã®å¦ç my $file = shift; unless ($file) { die "Usage: $0 file"; # å¼æ°ããªãã¨ãã¯ã使ç¨æ¹æ³ã示ãã¦çµäºã } # csvã®ååã«å¯¾å¿ãããã¼ãä½æ my $headers = [ 'name', 'age', 'country' ]; # ãã¡ã¤ã«ã解æãã¦csvå½¢å¼ã®ãã¼ã¿ãé åã®é åã«å¤æã # ãããã®ãã¼ãå¼æ°ã¨ãã¦æ¸¡ãã my @recs = parse_file($file, $headers); # åºå( ããã·ã¥ã®é åãªã®ã§ãforã§ãã©ã ) for my $items ( @recs ){ # ããã·ã¥ã¹ã©ã¤ã¹ãå©ç¨ãã¦é£çµ print join(',', @{$items}{@$headers} ) . "\n"; } # ãã¡ã¤ã«è§£æç¨ã®é¢æ°( ä»åã¯åã«æ¸ãæ»ãã ãã ããã©ã»ã» ) sub parse_file { my ($file, $headers) = @_; open(my $fh, "<", $file) or die "Cannot open $file for read: $!"; # è¤æ°ã®ã¬ã³ã¼ããæ ¼ç´ããé åã¸ã®ãªãã¡ã¬ã³ã¹ãæºå my $recs = []; while (my $line = <$fh>) { # æ¹è¡ãåãé¤ã chomp $line; # ãã¼ã¿ãæ ¼ç´ããããã·ã¥ã¸ã®ãªãã¡ã¬ã³ã¹ãæºå # ããã·ã¥ã¹ã©ã¤ã¹ãå©ç¨ãã¦ããã # ã«å¯¾å¿ãããã¼ã«ä»£å ¥ my $items = {}; @{$items}{@$headers} = split(',', $line); # pushé¢æ°ã®ç¬¬1å¼æ°ã¯é åãªã®ã§ã@$recsã¨ããªãã¡ã¬ã³ã¹ã push @$recs, $items; } close $fh; wantarray ? return @$recs : return $recs; }
以ä¸ã¯ãcsvãã¼ã¿ã®ãµã³ãã«ã§ããcsvãã¡ã¤ã«ãä½ã£ã¦ãã¹ã¯ãªããã®ç¬¬1å¼æ°ã«ä¸ãã¦å®è¡ãã¦ãã ããã
masao,10,Japan taro,20,USA rika,38,France
ã³ã¼ã解説
(1)ããã·ã¥ã®ãã¼ã®é åã®ä½æ
my $headers = ['name', 'age', 'country'];
ãããã·ã¥ã®ãã¼ã使ããã¨ã§ãååã¯åã«é åã§ãã£ããã®ã«æå³ãã¤ãã¦æãã¾ããcsvãã¡ã¤ã«ã®ä¸é ç®ç®ã¯nameã«ã2é ç®ç®ã¯ageã«ã3é ç®ç®ã¯countory ã«å¯¾å¿ãã¾ãã
(2)ãã¡ã¤ã«ãèªã¿è¾¼ãã§ãcsvå½¢å¼ã®ããã¹ããããã·ã¥ã®é åã«å¤æãã
my @recs = parse_file($file, $headers);
ãparse_fileã¯ããã¡ã¤ã«ãèªã¿è¾¼ãã§ãcsvå½¢å¼ã®ããã¹ããããã·ã¥ã®é åã«å¤æããèªä½ã®é¢æ°ã§ããå ¥åã¨åºåã®ã¤ã¡ã¼ã¸ã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
masao,10,Japan taro,20,USA rika,38,France â @recs = ( { name => 'masao', age => 10, country => 'Japan' }, { name => 'taro', age => 20, country => 'USA' }, { name => 'rika', age => 38, country => 'France' }, )
(3)ããã·ã¥ã®é åãåºåãã
for my $items (@recs) { print join(',', @{$items}{@$headers}) . "\n"; }
ãé åãªã®ã§å¤å´ã¯foreachã«ã¼ãã«ãªãã¾ããããã·ã¥ã¸ã®ãªãã¡ã¬ã³ã¹ã§ãã$itemsãjoin関数ã§ã«ã³ãã§é£çµãã¦åºåãã¾ãã
(3)-1 ããã·ã¥ã¹ã©ã¤ã¹ã®è§£èª¬
ãé£è§£ãªããã·ã¥ã¹ã©ã¤ã¹ @{$items}{@$headers} ã解説ãã¾ãã
ãã¾ã $items ã¯ããã·ã¥ã¸ã®ãªãã¡ã¬ã³ã¹ã§ã
{ name => 'masao', age => 10, country => 'Japan' }
ã®ããã«ãªã£ã¦ãã¾ãã
ãããªãã¡ã¬ã³ã¹ããã«ã¯æ®é %$items ã¨ãã¾ãããããã·ã¥ã¹ã©ã¤ã¹ãå©ç¨ã§ããããã«ããªãã¡ã¬ã³ã¹ãããã®ã§
@{$items}{ ããã·ã¥ã®ãã¼ã®ãªã¹ã }
ã¨ãã¾ãã
@{$items}{'name', 'age', 'country'}
ã¨æ¸ãã¨
('masao', 10, 'Japan')
ãåå¾ã§ãã¾ãã
ãã¾ãã$headers ã«ã¯ã['name', 'age', 'country'] ãä»£å ¥ããã¦ãã¾ãã®ã§ã@$headers ã¨ããªãã¡ã¬ã³ã¹ããã¨('name', 'age', 'country') ãåå¾ã§ãã¾ãã
@{$items}{@$headers}
ã¨ããè¨è¿°ã¯ã
@{$items}{'name', 'age', 'country'}
ã¨ããè¨è¿°ã¨åãã«ãªãã
('masao', 10, 'Japan')
ãåå¾ã§ãã¾ãã
join(',', @{$items}{@$headers})
ã¨ãã¦ãã«ã³ãã§çµã³ã¤ãã¦å®æã§ãã
(4)whileã«ã¼ãã§ã®å¦ç
while (my $line = <$fh>) { # æ¹è¡ãåãé¤ã chomp $line; # ãã¼ã¿ãæ ¼ç´ããããã·ã¥ã¸ã®ãªãã¡ã¬ã³ã¹ãæºå my $items = {}; # ããã·ã¥ã¹ã©ã¤ã¹ãå©ç¨ãã¦ããã # ã«å¯¾å¿ãããã¼ã«ä»£å ¥ @{$items}{@$headers} = split( ',', $line ); # pushé¢æ°ã®ç¬¬1å¼æ°ã¯é åãªã®ã§ã@$recs # ã¨ããªãã¡ã¬ã³ã¹ã push @$recs, $items; }
(4)-1 ããã·ã¥ã¸ã®ãªãã¡ã¬ã³ã¹ãç¨æ
my $items = {}; # ãã¼ã¿ãæ ¼ç´ããããã·ã¥ã¸ã®ãªãã¡ã¬ã³ã¹ãæºå
ãé åã®ä¸ã«æãããããã·ã¥ã¸ã®ãªãã¡ã¬ã³ã¹ãæºåãã¾ãã
(4)-2 ããã·ã¥ã¹ã©ã¤ã¹ã左辺ã«ããã¦å¯¾å¿ãããã¼ã«ä»£å ¥
@{$items}{@$headers} = split(',', $line);
ãä¸è¨ã§è§£èª¬ãããã¨ã¨åãã§ãã左辺ãããã·ã¥ã¹ã©ã¤ã¹ã«ãã¦ãããã«splité¢æ°ã§åå²ãããªã¹ããä»£å ¥ãã¾ãã
Perl
›
ãã¡ã¤ã«å ¥åºå
›
here