Brian Volk wrote:
> Hi All~

Hello,

> my program below is not returning any errors but nothing is happening to the
> .txt files like I hoped.  Can someone pls take a look and let me know what
> I'm doing wrong.  
>  
> ----- Thank you! ----
>  
> # If there is a .pdf file and a matching .txt file, open the .txt file and s
> %  http://.*  %  "$link"  %  
>  
> #!/usr/bin/perl 
>  
> use strict;
> use warnings;
> use File::Basename;
>  
> my $pdf_dir = "j:/flash_host/ecomm/descriptions/product/MSDS";
>     opendir(PDFDIR, $pdf_dir) or die "Can't open the $pdf_dir: $!\n";
>  
> # read file/directory names in that directory into @htmls
>  
> my @pdfs = readdir(PDFDIR) or die "Unable to read current dir:$!\n";

You are reading ALL entries from the directory into @pdfs (including the
. and .. entries.)


> closedir(PDFDIR);
>  
> my $text_dir = "c:/brian/descriptions/product/small";
>     opendir (TEXTDIR, $text_dir) or die "Can't open $text_dir: $!";
>  
> # read all the .txt files and load @ARGV for <> operator
>  
> @ARGV = map { "$text_dir/$_" } grep { !/^\./ } readdir TEXTDIR;
>  
> my %PDFDIR_LIST;
>  
> $PDFDIR_LIST{$_}=1 for @pdfs;
>  
> foreach my $text_file (<>) {

You are using a foreach loop with <>.  The empty readline operator <> is
"magical" in that it will open all the files listed in @ARGV and read
all the lines from all the files.  Because you are using a foreach loop
that means that all the lines from all the files have to be read and
stored in memory first before the foreach loop will iterate over them.
It also means that inplace edit using $^I won't work and that $. won't
give you the current line number and that $ARGV won't give you the
current file name.


>         my ($basename, $suffix) = fileparse($text_file,'.txt');

You are using File::Basename::fileparse() incorrectly, it returns three
values ($name,$path,$suffix) but since you are only using the first
value you haven't encountered a problem.  Also the second argument is a
regular expression so the period has to be escaped to match a period and
only the suffixes in the list you supply will be parsed.


>  my $link = "descriptions/product/small/$basename.pdf";
>  
>  if( $PDFDIR_LIST{"$basename.pdf"} ){
>  open FH, $text_file or die "can't open $text_file: $!";
>         s% http://.* % $link %;
>         next;}
>  
>  }
> close (FH);
> closedir (TEXTDIR);
>  
> __END__


If I understand correctly then something like this should work (untested):


#!/usr/bin/perl
use strict;
use warnings;
use File::Basename;

my $pdf_dir = 'j:/flash_host/ecomm/descriptions/product/MSDS';
opendir PDFDIR, $pdf_dir or die "Can't open the $pdf_dir: $!\n";

# read file/directory names in that directory into %PDFDIR_LIST
my %PDFDIR_LIST = map { /^(.+)\.pdf$/i ? ( $1, 1 ) : () } readdir PDFDIR
closedir PDFDIR;

my $text_dir = 'c:/brian/descriptions/product/small';
opendir TEXTDIR, $text_dir or die "Can't open $text_dir: $!";

# read all the .txt files and load @ARGV for <> operator
@ARGV = map "$text_dir/$_", grep /\.txt$/i, readdir TEXTDIR;
closedir TEXTDIR;

$^I = '.bak';  # use inplace edit and backup the original

my $link;
while ( <> ) {
    if ( $. == 1 ) {  # first line of file
        my ( $basename ) = fileparse( $ARGV, /\.txt/i );

        if ( not exists $PDFDIR_LIST{ $basename } ) {
            close ARGV;  # close the current filehandle
            next;        # and go on to the next file
            }

        $link = "descriptions/product/small/$basename.pdf";
        }

    s% http://.* % $link %g;
    print;
    close ARGV if eof;  # reset the $. variable
    }

__END__



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