Home > Archive > PERL Beginners > August 2005 > substitution doesn't work on all files ?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
substitution doesn't work on all files ?
|
|
| Brian Volk 2005-08-06, 4:00 am |
| Hi All,
Now that I have my program working ( much thanks to the group ) I'm totally
stumped as to why the substitution only works on some of the .txt files and
not all. I'm attaching two files.. 70119.txt and 30700.txt. The s/ /
/; in the program below works for file 70119.txt and not for file
30700.txt.. and I have no idea why... :~) If anyone can tell me why this
is happening, I would really appreciate it!
Thanks again!
__BEGIN__
#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;
use File::Copy;
my $pdf_dir = "j:/flash_host/ecomm/descriptions/product/MSDS";
# read file/directory names in that directory into @pdfs
opendir PDFDIR, $pdf_dir
or die "Can't opendir $pdf_dir: $!\n";
my @pdfs = readdir(PDFDIR);
closedir PDFDIR;
my $text_dir = "c:/brian/descriptions/product/small";
# my $text_dir = "j:/flash_host/ecomm/descriptions/product/small";
opendir TEXTDIR, $text_dir
or die "Can't opendir $text_dir: $!";
my @txts = map { "$text_dir/$_" } grep { !/^\./ } readdir TEXTDIR;
closedir TEXTDIR;
my %PDFDIR_LIST;
$PDFDIR_LIST{$_}=1 for @pdfs;
foreach my $text_file (@txts) {
my ($basename, $suffix) = fileparse($text_file,'.txt');
my $link = "descriptions/product/MSDS/$basename.pdf";
if( $PDFDIR_LIST{"$basename.pdf"} ) {
my $out_file = "$text_file.new";
open INPUT, "< $text_file"
or die "can't open $text_file: $!";
open OUTPUT, "> $out_file"
or die "can't open $out_file: $!";
while (<INPUT> ) {
s% http://.* % $link %g;
print OUTPUT;
}
close INPUT; close OUTPUT;
move( $text_file, "$text_file.bak" );
move( $out_file, $text_file );
}
}
__END__
Brian Volk
HP Products
317.298.9950 x1245
<mailto:bvolk@hpproducts.com> bvolk@hpproducts.com
| |
| Jeff 'japhy' Pinyan 2005-08-06, 4:59 pm |
| On Aug 5, Brian Volk said:
> Now that I have my program working ( much thanks to the group ) I'm totally
> stumped as to why the substitution only works on some of the .txt files and
> not all. I'm attaching two files.. 70119.txt and 30700.txt. The s/ /
> /; in the program below works for file 70119.txt and not for file
> 30700.txt.. and I have no idea why... :~) If anyone can tell me why this
> is happening, I would really appreciate it!
I would add debugging statements to make sure the loop is going over all
the files you expect it to.
> if( $PDFDIR_LIST{"$basename.pdf"} ) {
> my $out_file = "$text_file.new";
> open INPUT, "< $text_file"
> or die "can't open $text_file: $!";
> open OUTPUT, "> $out_file"
> or die "can't open $out_file: $!";
warn "PDFDIR_LIST had $basename.pdf; operating on $text_file\n";
> while (<INPUT> ) {
> s% http://.* % $link %g;
> print OUTPUT;
> }
> close INPUT; close OUTPUT;
> move( $text_file, "$text_file.bak" );
> move( $out_file, $text_file );
> }
> }
--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
|
|
|
|
|