Skip to content

Instantly share code, notes, and snippets.

@luelista
Created February 25, 2011 11:35
Show Gist options
  • Save luelista/843680 to your computer and use it in GitHub Desktop.
Save luelista/843680 to your computer and use it in GitHub Desktop.
Konvertiert Adress-Datensätze zwischen den zwei komischen Datenformat aus dem Info-Unterricht...
#!/usr/bin/perl
use Data::Dumper;
use strict;
my @felder = qw(Name Vorname Strasse Hausnr PLZ Ort);
sub write_to_file { my %data = %{$_[0]}; my @contacts = @{$_[1]};
print $data{$_}, "|" for (@felder);
print $_, "|" for (@contacts);
print "\n";
}
sub convert_to_moritzformat {
my %data = (); my @contacts = ();
while (<>) {
%data = @contacts = () if ($_ =~ /^\[Person\]/);
write_to_file(\%data, \@contacts) if ($_ =~ /^\[\/Person\]/);
push @contacts, ($1, $2) if ($_ =~ /^Anschluss=([0-9]+);(.*?)\s*$/);
$data{$1} = $2 if ($_ =~ /^([A-Za-z0-9_-]+)=(.*?)\s*$/);
}
exit;
}
while(<>) {
convert_to_moritzformat if ($_ =~ /\[Person\]/);
next if ($_ =~ /^\s*$/);
my @data = split /\|/;
print "[Person]\n";
for(0..$#felder) {
print $felder[$_], "=", $data[$_], "\n";
}
for(my $i=$#felder+1; $i<$#data; $i+=2) {
print "Anschluss=", $data[$i], ";", $data[$i+1], "\n";
}
print "[/Person]\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment