「3-3-2.複数のファイルをまとめて、1つのファイルにする」とは逆に1つの入力ファイルから複数の出力ファイルに振り分ける方法について解説します。ここでは、日本郵政公社が提供している郵便番号データを都道府県別に出力する方法を例にとって説明します。郵便番号データは下記のサイトにあるので参照してください。
http://www.post.japanpost.jp/zipcode/
入力データの1項目目はJISで定められた都道府県コード(2バイト)と市区町村コード(3バイト)の2つを組み合わせた5バイトのデータになっています。そこで、入力データの1項目目から先頭2バイトを取り、そのデータに基づいて、出力ファイルを分けて出力します。
具体的には、【スクリプト】その1を見ていただければわかるように、Perlでは、open命令およびclose命令ともに引数に変数を指定することができます。「open($w_out,"$w_file");」や「close($w_out);」のような形式になりますが、入力データの1つとして、県別の出力ファイル名を指定したファイルをあらかじめ用意しておくことで、スクリプト自体がすっきりとした形になります。open命令やclose命令を定数で指定した場合、どのようになるかを【スクリプト】その2で提示していますので、比較すると、どのくらい簡潔になるかがわかるでしょう。入出力ファイルが増えてきた場合、このように変数で指定することでスクリプトがすっきりと表示できるようになります。
01101,"060 ","0600000","ホッカイドウ","サッポロシチュウオウク","イカニケイサイガナイバアイ","北海道","札幌市中央区","以下に掲載がない場合",0,0,0,0,0,0 01101,"064 ","0640941","ホッカイドウ","サッポロシチュウオウク","アサヒガオカ","北海道","札幌市中央区","旭ケ丘",0,0,1,0,0,0
01,01HOKKAIDO.CSV 02,02AOMORI.CSV 03,03IWATE.CSV 04,04MIYAGI.CSV 05,05AKITA.CSV 06,06YAMAGATA.CSV 07,07FUKUSHIMA.CSV 08,08IBARAKI.CSV 09,09TOCHIGI.CSV 10,10GUMMA.CSV 11,11SAITAMA.CSV 12,12CHIBA.CSV 13,13TOKYO.CSV 14,14KANAGAWA.CSV 15,15NIIGATA.CSV 16,16TOYAMA.CSV 17,17ISHIKAWA.CSV 18,18FUKUI.CSV 19,19YAMANASHI.CSV 20,20NAGANO.CSV 21,21GIFU.CSV 22,22SHIZUOKA.CSV 23,23AICHI.CSV 24,24MIE.CSV 25,25SHIGA.CSV 26,26KYOTO.CSV 27,27OSAKA.CSV 28,28HYOGO.CSV 29,29NARA.CSV 30,30WAKAYAMA.CSV 31,31TOTTORI.CSV 32,32SHIMANE.CSV 33,33OKAYAMA.CSV 34,34HIROSHIMA.CSV 35,35YAMAGUCHI.CSV 36,36TOKUSHIMA.CSV 37,37KAGAWA.CSV 38,38EHIME.CSV 39,39KOCHI.CSV 40,40FUKUOKA.CSV 41,41SAGA.CSV 42,42NAGASAKI.CSV 43,43KUMAMOTO.CSV 44,44OITA.CSV 45,45MIYAZAKI.CSV 46,46KAGOSHIMA.CSV 47,47OKINAWA.CSV
# ken1.pl # 内容 : 郵便番号データを県別に分割するスクリプト # Copyright (c) 2002 Mitsuo Minagawa, All rights reserved. # (minagawa@fb3.so-net.ne.jp) # 使用方法 : c:\>perl ken1.pl # open(IN1,"ken_all.csv"); open(IN2,"ken.txt"); while ($line1 = <IN2>) { chomp($line1); @in2 = split(",",$line1,-1); $w_out = OUT.$in2[0]; $w_file = ">".$in2[1]; open($w_out,"$w_file"); } while ($line1 = <IN1>) { chomp($line1); # CSV形式の $line1 から値を取り出して @in1 に入れる my $temp = $line1; $temp =~ s/(?:\x0D\x0A|[\x0D\x0A])?$/,/; @in1 = map {/^"(.*)"$/ ? scalar($_ = $1, s/""/"/g, $_) : $_} ($temp =~ /("[^"]*(?:""[^"]*)*"|[^,]*),/g); $w_ken = substr($in1[0],0,2); $w_out = "OUT".$w_ken; print $w_out "$line1\n"; } close(IN1); while ($line1 = <IN2>) { chomp($line1); @in2 = split(",",$line1,-1); $w_out = OUT.$in2[0]; close($w_out); } close(IN2);
# ken2.pl # 内容 : 郵便番号データを県別に分割するスクリプト # Copyright (c) 2002 Mitsuo Minagawa, All rights reserved. # (minagawa@fb3.so-net.ne.jp) # 使用方法 : c:\>perl ken2.pl # open(IN1,"ken_all.csv"); open(OUT01,">01HOKKAIDO.CSV"); open(OUT02,">02AOMORI.CSV"); open(OUT03,">03IWATE.CSV"); open(OUT04,">04MIYAGI.CSV"); open(OUT05,">05AKITA.CSV"); open(OUT06,">06YAMAGATA.CSV"); open(OUT07,">07FUKUSHIMA.CSV"); open(OUT08,">08IBARAKI.CSV"); open(OUT09,">09TOCHIGI.CSV"); open(OUT10,">10GUMMA.CSV"); open(OUT11,">11SAITAMA.CSV"); open(OUT12,">12CHIBA.CSV"); open(OUT13,">13TOKYO.CSV"); open(OUT14,">14KANAGAWA.CSV"); open(OUT15,">15NIIGATA.CSV"); open(OUT16,">16TOYAMA.CSV"); open(OUT17,">17ISHIKAWA.CSV"); open(OUT18,">18FUKUI.CSV"); open(OUT19,">19YAMANASHI.CSV"); open(OUT20,">20NAGANO.CSV"); open(OUT21,">21GIFU.CSV"); open(OUT22,">22SHIZUOKA.CSV"); open(OUT23,">23AICHI.CSV"); open(OUT24,">24MIE.CSV"); open(OUT25,">25SHIGA.CSV"); open(OUT26,">26KYOTO.CSV"); open(OUT27,">27OSAKA.CSV"); open(OUT28,">28HYOGO.CSV"); open(OUT29,">29NARA.CSV"); open(OUT30,">30WAKAYAMA.CSV"); open(OUT31,">31TOTTORI.CSV"); open(OUT32,">32SHIMANE.CSV"); open(OUT33,">33OKAYAMA.CSV"); open(OUT34,">34HIROSHIMA.CSV"); open(OUT35,">35YAMAGUCHI.CSV"); open(OUT36,">36TOKUSHIMA.CSV"); open(OUT37,">37KAGAWA.CSV"); open(OUT38,">38EHIME.CSV"); open(OUT39,">39KOCHI.CSV"); open(OUT40,">40FUKUOKA.CSV"); open(OUT41,">41SAGA.CSV"); open(OUT42,">42NAGASAKI.CSV"); open(OUT43,">43KUMAMOTO.CSV"); open(OUT44,">44OITA.CSV"); open(OUT45,">45MIYAZAKI.CSV"); open(OUT46,">46KAGOSHIMA.CSV"); open(OUT47,">47OKINAWA.CSV"); while ($line1 = <IN1>) { chomp($line1); # CSV形式の $line1 から値を取り出して @in1 に入れる my $temp = $line1; $temp =~ s/(?:\x0D\x0A|[\x0D\x0A])?$/,/; @in1 = map {/^"(.*)"$/ ? scalar($_ = $1, s/""/"/g, $_) : $_} ($temp =~ /("[^"]*(?:""[^"]*)*"|[^,]*),/g); $w_ken = substr($in1[0],0,2); $w_out = "OUT".$w_ken; print $w_out "$line1\n"; } close(IN1); close(OUT01); close(OUT02); close(OUT03); close(OUT04); close(OUT05); close(OUT06); close(OUT07); close(OUT08); close(OUT09); close(OUT10); close(OUT11); close(OUT12); close(OUT13); close(OUT14); close(OUT15); close(OUT16); close(OUT17); close(OUT18); close(OUT19); close(OUT20); close(OUT21); close(OUT22); close(OUT23); close(OUT24); close(OUT25); close(OUT26); close(OUT27); close(OUT28); close(OUT29); close(OUT30); close(OUT31); close(OUT32); close(OUT33); close(OUT34); close(OUT35); close(OUT36); close(OUT37); close(OUT38); close(OUT39); close(OUT40); close(OUT41); close(OUT42); close(OUT43); close(OUT44); close(OUT45); close(OUT46); close(OUT47);
入力データ(県別の出力ファイル名)のサンプルはこちらにあります。