Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@juriad
Created October 9, 2012 08:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juriad/3857329 to your computer and use it in GitHub Desktop.
Save juriad/3857329 to your computer and use it in GitHub Desktop.
Hledám program pro Karel.Pospisil.8
use strict;
use warnings;
use feature qw(say switch);
use Data::Dumper;
my %errors = ( "format" => "Wrong format of line", "duplicate key" => "Duplicate key of line" );
if(scalar @ARGV != 4) {
say "script requires four arguments (two input files, two output files)";
say "copy.pl <main.txt> <old.txt> <new.txt> <error.txt>";
exit(1);
}
my ($main, $old, $new, $error) = @ARGV;
open(FILE, '<', $main) or die("Unable to open file ", $main);
my @mainLines = <FILE>;
close(FILE);
#last line is blank
if($mainLines[-1] =~ m/^\s*$/) {
pop @mainLines;
}
my $ignoreErrorsInMain = 0;
my @errorsInMain;
my %mainMap;
for my $i ((0..@mainLines-1)) {
my $line = $mainLines[$i];
my $error = 0;
if($line !~ /^""""".+"""""=.*$/) {
if (!$ignoreErrorsInMain) {
say "Wrong format of line ", $i+1, " in ", $main;
say "What shall I do? [Ignore (default), All ignore, Quit]";
my $todo = <STDIN>;
given($todo) {
when(/^A/i) {
$ignoreErrorsInMain = 1;
}
when(/^Q/i) {
exit(1);
}
default {
}
}
}
push @errorsInMain, {line => ($i+1), error => "format"};
next;
}
my ($key, $value) = split /=/, $line, 2;
$key =~ s/^"""""//;
$key =~ s/"""""$//;
$value =~ s/\R//g;
if (defined $mainMap{$key}) {
if (!$ignoreErrorsInMain) {
say "Repeating key ", $key, " on line ", $i+1, " in ", $main;
say "What shall I do? [Ignore (default), All ignore, Quit]";
my $todo = <STDIN>;
given($todo) {
when(/^A/i) {
$ignoreErrorsInMain = 1;
}
when(/^Q/i) {
exit(1);
}
default {
}
}
}
push @errorsInMain, { line => ($i+1), error => "duplicate key" };
next;
}
$mainMap{$key} = {key => $key, line => ($i+1), value => $value};
}
say "processed file ", $main;
open(FILE, '<', $old) or die("Unable to open file ", $old);
my @oldLines = <FILE>;
close(FILE);
#last line is blank
if($oldLines[-1] =~ m/^\s*$/) {
pop @oldLines;
}
my $ignoreErrorsInOld= 0;
my @errorsInOld;
my %oldMap;
for my $i ((0..@oldLines-1)) {
my $line = $oldLines[$i];
my $error = 0;
if($line !~ /^.+=.*$/) {
if (!$ignoreErrorsInOld) {
say "Wrong format of line ", $i+1, " in ", $old;
say "What shall I do? [Ignore (default), All ignore, Quit]";
my $todo = <STDIN>;
given($todo) {
when(/^A/i) {
$ignoreErrorsInOld = 1;
}
when(/^Q/i) {
exit(1);
}
default {
}
}
}
push @errorsInOld, {line => ($i+1), error => "format"};
next;
}
my ($key, $value) = split /=/, $line, 2;
$value =~ s/\R//g;
if (defined $oldMap{$key}) {
if (!$ignoreErrorsInMain) {
say "Repeating key ", $key, " on line ", $i+1, " in ", $old;
say "What shall I do? [Ignore (default), All ignore, Quit]";
my $todo = <STDIN>;
given($todo) {
when(/^A/i) {
$ignoreErrorsInOld = 1;
}
when(/^Q/i) {
exit(1);
}
default {
}
}
}
push @errorsInOld, { line => ($i+1), error => "duplicate key" };
next;
}
$oldMap{$key} = {key => $key, line => ($i+1), value => $value};
}
say "processed file ", $old;
if(scalar @errorsInMain || scalar @errorsInOld) {
say "There are some errors:";
if(scalar @errorsInMain) {
say scalar @errorsInMain, " in ", $main;
}
if(scalar @errorsInOld) {
say scalar @errorsInOld, " in ", $old;
}
say "What shall I do? [Write (default), Ignore, Quit]";
my $todo = <STDIN>;
given ($todo) {
when(/^I/i) {
}
when(/^Q/i) {
exit(1);
}
default {
open(FILE, '>', $error) or die("Unable to open file ", $error);
say FILE "Errors in ", $main, ":";
foreach my $line (@errorsInMain) {
say FILE "$line->{line}: $errors{$line->{error}}";
}
say FILE "Errors in ", $old, ":";
foreach my $line (@errorsInOld) {
say FILE "$line->{line}: $errors{$line->{error}}";
}
close(FILE);
}
}
say "Do you want to continue? (Will start writing data to ", $new, ") [No (default), Yes]";
$todo = <STDIN>;
given($todo) {
when(/^Y/i) {
}
default {
exit(1);
}
}
}
my @lines = sort {$a->{line} <=> $b->{line} } values %oldMap;
open(FILE, '>', $new) or die("Unable to open file", $new);
for my $line (@lines) {
my $key = $line->{key};
my $value = $line->{value};
if(defined $mainMap{$key}) {
$value = $mainMap{$key}->{value};
delete $mainMap{$key};
}
say FILE $key, "=", $value;
}
if ( scalar keys %mainMap > 0 ) {
say "There are ", scalar keys %mainMap," unprocessed lines in ", $main;
say "what shall I do? [Ignore (default), Append]";
my $todo = <STDIN>;
given($todo) {
when (/^A/i) {
@lines = sort {$a->{line} <=> $b->{line} } values %mainMap;
for my $line (@lines) {
my $key = $line->{key};
my $value = $line->{value};
say FILE $key, "=", $value;
}
}
default {
}
}
}
close(FILE);
say "Data has been written to ", $new;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment