shoura has asked for the wisdom of the Perl Monks concerning the following question:

I have two file a.txt contain a text like this : The Original series began in 1864 and continues to the present. (...) The second file b.txt is sequence of words (lines contains 5, 4, 3 or 2 words) like this :
series good nice summer night present friends ....
The result file r.txt should be : The Original series began in 1864 and continues to the present  serieszzgoodzznicezzsummer nightzzpresentzzfriends. (...) My problem is that my script doesn't read all lines in file a.txt and in the result this add "zz" just between two first words
#!/usr/bin/perl open FILE1,"./a.txt" or die "Cannot open a.txt"; open FILE2,"./b.txt" or die "Cannot open b.txt"; open FILE3,">./r.txt" or die "Cannot create r.txt"; while (<FILE1>) { chomp; /[^\ ]*$/; $common = $&; $begin = $`; chop $begin; $array{$common} = $begin; } close FILE1; while (<FILE2>) { chomp; /^[^\ ]*\ /; $common = $&; chop $common; $end = $'; print FILE3 "$array{$common} $common\zz$end" if exists $array{$commo +n}; } close FILE2; close FILE3;

Replies are listed 'Best First'.
Re: Insert and attach sequence of words in txt file if it exist in other file
by poj (Abbot) on May 13, 2017 at 19:43 UTC

    Try reading the b file first and then match the words to those in a

    #!/usr/bin/perl use strict; use Data::Dumper; my %file2 = (); open FILE2,'<',"./b.txt" or die "Cannot open b.txt"; while (<FILE2>){ my @words = split /\s+/,$_; my $zz = join 'zz',@words; for (@words){ $file2{$_} = $zz; } } close FILE2; print Dumper \%file2; open FILE1,'<',"./a.txt" or die "Cannot open a.txt"; open FILE3,'>',"./r.txt" or die "Cannot create r.txt"; while (<FILE1>){ chomp; my @words = split /[ \.]/,$_; my @added; for (@words){ push @added,$file2{$_} if exists $file2{$_}; }; print FILE3 $_,(join ' ',@added),"\n"; } close FILE1; close FILE3;
    poj
      this work thanks but here it copie sequence of words after . but me i want that it insert it before . i have some ligne in the a.txt that is like this <title> (here text)...</title > so in this case the sequence should be copied before </title>. should i change my @words = split / \./,$_; ?

        Is the . always at the end of the line ?