#!/usr/local/bin/perl # Load a conversion table from CONVTABLE to %ConvTable. Then find matches in a file and convert them. use strict; use warnings; use Encode; use 5.014; use utf8; use diagnostics; use autodie; use warnings qw< FATAL utf8 >; use open qw< :std :utf8 >; use charnames qw< :full >; use feature qw< unicode_strings >; my ($i,$j,$InputFile, $OutputFile,$word,$from,$to,$linetoprint); my (@line, @lineout); my %ConvTable; # Conversion hash my ($CONVTABLE, $INPUT, $OUTPUT); print 'Conversion table: opening file: E:\My Documents\Technical\Perl\Conversion table 1.txt'."\n"; my $sta= open ($CONVTABLE, "<:encoding(utf8)", 'E:\My Documents\Technical\Perl\Conversion table 1.txt'); binmode STDOUT, ':utf8'; # output should be in UTF-8 binmode $DB::OUT, ':utf8' if $DB::OUT; # for the debugger # Load conversion hash while (<$CONVTABLE>) { chomp; print "$_\n"; @line = split; $/=','; chomp(@line); # Chomp chomps a variable if its last char equals $/ $/="\n"; # Restore $/ $ConvTable{$line[0]}=$line[1]; } # end while (<$CONVTABLE>) while ( ($i,$j) = each (%ConvTable) ) { print "$i => $j\n"; } # Open file to convert print "Input to convert: enter path\\fileName: "; $InputFile = ; chomp($InputFile); print "\$InputFile=\"$InputFile\"\n"; $sta = open ($INPUT, "<:encoding(utf8)", $InputFile); # Open output file print "OUTPUT: enter path and fileName: "; $OutputFile = ; chomp($OutputFile); print "\$OutputFile= \"$OutputFile\"\n"; $sta = open ($OUTPUT, ">:encoding(utf8)", ">$OutputFile"); # Iterate over lines of INPUT, convert according to %ConvTable, store in OUTPUT while (<$INPUT>) { chomp; @line = split; foreach $word(@line) { while (($from, $to) = each(%ConvTable)) { # traverse the conversion table. # check if there are any matches in the word and substitue $word =~ s/$from/$to/; # substitute substrings if match } # end value in %ConvTable push(@lineout, $word); } # end foreach string in the line $linetoprint = join (", ",@lineout); print ($OUTPUT, "$linetoprint\n"); $#lineout= (-1); # empty @lineout } # end while over INPUT file