Vincent Li am Dienstag, 10. Januar 2006 19.59:
> Hi List:

Hi Vincent

> 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:

Which is not easy to test, so I didn't :-)

> #!/usr/bin/perl
> use strict;
> use warnings;

Great two lines!

> 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!";
>
> for (@array2) {
>     my $string = $_;

shorter: for my $string (@array2) {

>        for (@array1) {
>            if (/$string/) {
>                 s/$_/#$_/;

Eventually it is faster just to handle the beginning of the line 
with s/^./#./

>             }
>         }
> }
>
> It did not work as I wish, any thoughts or other method to do this?

How did it not work as expected?

You pass through the whole @array1 for every value in @array2, this is much 
work. If the lines in file1 are unique and not very numerous, you could use a 
hash (line_content=>line_number) instead of @array1, modify it and write it 
back to file at the end. In this case, a substitution would not be necessary,  
'#' could simply be prepended (i _think_ this is faster than a subst).

There may also be a useful module on search.cpan.org.

hth, 
joe

-- 
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