Vincent Li wrote:
> Hi List:

Hello,

> I have two files like this:
> 
> file1:
> score CN_SUBJ_PROMOTE                3.100 # [0.000..3.100]
> score CN_SUBJ_PROMOTION              3.600 # [0.000..3.600]
> score CN_SUBJ_PROVIDE                3.000 # [0.000..3.000]
> 
> file2:
> CN_SUBJ_PROMOTE
> CN_SUBJ_PROMOTION
> 
> If string CN_SUBJ_PROMOTE exist in file2 and file1, add comment (#) to
> file1 like:
> 
> #score CN_SUBJ_PROMOTE                3.100 # [0.000..3.100]
> #score CN_SUBJ_PROMOTION              3.600 #[0.000..3.600]
> 
> I came  up with this script:
> 
> #!/usr/bin/perl
> use strict;
> use warnings;
> use Tie::File;
> 
> tie my @array1, ,'Tie::File',  "file1" or die "Could not tie file1!";
> tie my @array2, ,'Tie::File',  "file2" or die "Could not tie file2!";
                ^^^
You have an extra comma there.  You should include the $! variable in the
error message so you know why it failed.


> for (@array2) {
>    my $string = $_;
>       for (@array1) {
>           if (/$string/) {
>                s/$_/#$_/;
>            }
>        }
> }
> 
> It did not work as I wish, any thoughts or other method to do this?

You are using the string 'score CN_SUBJ_PROMOTE                3.100 #
[0.000..3.100]' as a regular expression but the regular expression will never
match the string.  For example the character class [0.000..3.100] will not
match the string '[0.000..3.100]' because there are no '[' and ']' characters
in the character class.  You probably want something like:

for my $string ( @array1 ) {
    for my $pattern ( @array2 ) {
        if ( /\b\Q$pattern\E\b/ ) {
            $string = "#$string";
            }
        }
    }



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to