Skip to content

Instantly share code, notes, and snippets.

@kzys

kzys/splitvcf.pl

Created Aug 3, 2008
Embed
What would you like to do?
# Originally written by ono_matope
# http://d.hatena.ne.jp/ono_matope/20080713#1215974472
use strict;
use warnings;
use utf8;
use Encode;
use Unicode::Japanese;
binmode(STDOUT, 'encoding(utf8)');
sub read_categories {
my ($path) = @_;
my %result;
open(my $file, '<:encoding(shiftjis)', $path);
my $category;
my $buffer;
while (my $line = <$file>){
$line =~ s/\r\n$//g;
if ($line =~ /^X-GN:(.*)$/) {
$category = $1;
}
elsif ($line =~ /^SORT\-STRING:([^\x20]+)\x20([^\x20]+)$/) {
$line = "X-PHONETIC-LAST-NAME:$1\nX-PHONETIC-FIRST-NAME:$2";
}
elsif ($line =~ /^N:([^\s]+)\s([^\s;]+);;;;/) {
$line = "N:$1;$2;;;";
}
else {
$line =~ s/^SORT\-STRING/X-PHONETIC-LAST-NAME/;
$line =~ s/^X\-GNO.*//;
$line =~ s/^X\-REDUCTION.*//;
$line =~ s/^CLASS.*//;
}
if ($line) {
$buffer .= Unicode::Japanese->new("$line\n")->h2zKana->getu;
}
if ($line =~ /^END:VCARD$/) {
$result{ $category } .= "$buffer\n";
$buffer = '';
}
}
close($file);
return %result;
}
my $path = shift;
my %cards_of = read_categories($path);
foreach my $category (keys %cards_of){
open(my $file, '>:encoding(shiftjis)', "$category.vcf");
print $file $cards_of{ $category };
close($file);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.