> -----Original Message----- > From: Jay Savage [mailto:[EMAIL PROTECTED] > Sent: Thursday, August 04, 2005 12:46 PM > To: Brian Volk; beginners perl > Subject: Re: match basename file and s / / /; > > > On 8/4/05, Brian Volk <[EMAIL PROTECTED]> wrote: > > > > > [snip] > > Thank you for the reply but I don't think I understand.. > The .txt files > > that I am loading into @ARGV are the files that I want to open and > > substitute the URL for a local path.... I thought that I > had to include the > > foreach $text_file so I could match the basename of the > .txt file and .pdf > > file and then include the "$basename.pdf" in the $link... > I'm pretty sure I > > am matching the file up correctly but the .txt file are > being erased instead > > of sub'ing the url w/ the local path... I think I'm > confused... :~) > > > > $ARGV, not $_, is the value of the current open file when using <>. In > scalar context <> returns individual lines. It also concatentates > @ARGV, treating the listed files as essentially one file (at least > most of the time; it possible to find out what file you're in; read > perlop). In list context, it returns a list of lines from the > concatentated @ARGV. So, let's say I have three files in @ARGV, a.a, > b.b, and c.c, each ten lines long. > > while (<>) { > #cycles through 30 times, not stopping between files > } > > @lines = <>; > #returns a single list of all the lines in the files > # with $lines[0] being the first line of a.a > # and $lines[29] being the last line of c.c > > What you probably want here is: > > foreach my $text_file (@ARGV) { > > # ...and then the rest of your code looks like it should work. > > In you current code, the block executes once for each line of each > file, and the variable $text_file = 'a line from whichever file you > happen to be in at the moment'. Since 'a line from whichever file you > happen to be in at the moment'.pdf almost certainly doesn't exist, > nothing happens. But you've set $^|, so when <> opens each file it is > renamed $ARGV.bak and an empty file $ARGV is created. > > Key point: <> != @ARGV (!= $ARGV) > > HTH > > -- jay
Jay, Thank you very much for the reply and the detailed explanation. I learned a lot. Unfortunately, I think there is something else wrong. When I execute the program nothing happens. I'm just guessing but I think my problem lies somewhere w/ this line if( $PDFDIR_LIST{"$basename.pdf"} ) { If I take out the .pdf and quotes I don't get the Use of uninitialized value error but still nothing happens.. Anyway, thanks again for explanation but for now it's back to the drawing board and waiting for that light in my head to go off! :~) Brian #!/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 @pdfs my @pdfs = readdir(PDFDIR) or die "Unable to read current dir:$!\n"; 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; $^I = '.bak'; foreach my $text_file (@ARGV) { my ($basename, $suffix) = fileparse($text_file,'.txt'); my $link = "descriptions/product/small/$basename.pdf"; if( $PDFDIR_LIST{"$basename.pdf"} ) { s% http://.* % $link %; print; next; } } closedir (TEXTDIR); __END__ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>