in reply to Re: Compare two file text input, compare it, replace and write new file
in thread Compare two file text input, compare it, replace and write new file

OK the newest code :
#!/usr/bin/perl use strict; use warnings; my $dfile = 'data'; my $sfile = 'source.txt'; my $tfile = 'target.html'; open DFILE, '<', $dfile or die "can not open '$dfile' because: $!"; open SFILE, '<', $sfile or die "can not open '$sfile' because: $!"; open TFILE, '>', $tfile or die "can not open '$tfile' because: $!"; my %words; while (<DFILE>) { chomp; my ($key, $val) = split /:/; $words{$key} = $val; }; while ( <SFILE> ) { my @words = split /(?=\\)/; for my $word ( @words ) { $word = $words{ $word } if exists $words{ $word }; } print TFILE "@words\n<br>"; } close(SFILE); close(DFILE); close(TFILE);
having source.txt :
\ca\ra\ka\tl \ca\ra\ka\tl
the last character of line (\tl) not replace by value on data. The result in target.html is :
&#xa15 &#xa16 &#xa17 \tl <br>&#xa15 &#xa916 &#xa17 \tl

Replies are listed 'Best First'.
Re^3: Compare two file text input, compare it, replace and write new file
by wa2nlinux (Novice) on Feb 15, 2012 at 13:12 UTC
    Solved by adding chomp
    while ( <SFILE> ) { chomp my @words = split /(?=\\)/; for my $word ( @words ) { $word = $words{ $word } if exists $words{ $word }; } print TFILE "@words\n<br>"; }
    another question: is it OK, if I used hash array my "data" (DFILE) on that program contain about 5000 lines ?
    my %words; while (<DFILE>) { chomp; my ($key, $val) = split /:/; $words{$key} = $val; };