> -----Original Message-----
> From: John W. Krahn [mailto:[EMAIL PROTECTED]
> Sent: Monday, August 21, 2006 5:49 PM
> To: Perl Beginners
> Subject: Re: grab first item in every file
>
> Brian Volk wrote:
> > Hi All~
>
> Hello,
>
> > How can I grab the first item in every file and include in the first
> > print statement below. The first item will always start w/ a "$" and
> > will not match a key in %DIR_LIST. The other items in the file (.pdf's)
> > may or may not match a key in %DIR_LIST
>
> It looks like you need something like this:
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> # open the directory that contains all the MSDS in .pdf format
> my $pdf_dir = '/var/www/html/msds';
> opendir DIR, $pdf_dir or die "Can't open the $pdf_dir: $!\n";
>
> # read file names in that directory into @pdfs
> my @pdfs = grep /\.pdf\z/i, readdir DIR
> or die "Unable to read current dir:$!\n";
> my %DIR_LIST = map { $_ => 1 } @pdfs;
> closedir DIR;
>
> # open all the .TXT file from the 400
> my $orders_dir = '/home/bvolk/msds_in';
> opendir ORDERS, $orders_dir or die "Can't open $orders_dir: $!";
>
> # read all the item numbers in all the .txt files w/ map and
> # load @ARGV for <> operator
> @ARGV = map "$orders_dir/$_", grep !/^\./, readdir ORDERS;
>
> my ( $job, $truck );
> while ( <> ) {
> ( $job ) = $ARGV =~ /(\d+)/ if $. == 1;
> $truck = $1 if /\A\$(\d+)/;
> next unless /\A((.+)\.pdf)\z/;
> my ( $key, $msds ) = ( $1, $2 );
>
> if ( exists $DIR_LIST{ $key } ) {
> my $indy = 10;
> print "Job $job printing msds $msds to $indy on truck #$truck\n";
> }
> else {
> print "Job $job missing msds $msds\n";
> }
> }
>
> __END__
>
>
>
> John
> --
> use Perl;
> program
> fulfillment
>
> --
John,
Thank you for the reply, I understand most of it: :-) I'm now having a
problem getting the $msds to print. Below is a sample of a text file and the
program w/ your changes.
$job and $truck print but $msds won't print in either case.
Filename1: O20270.TXT
$432
12345.pdf
12121.pdf
13131.pdf
44444.pdf
Filename2: O20271.TXT
$433
12345.pdf
12000.pdf
#!/usr/bin/perl
use strict;
use warnings;
# open the directory that contains all the MSDS in .pdf format
my $pdf_dir = '/var/www/html/msds';
opendir DIR, $pdf_dir or die "Can't open the $pdf_dir: $!\n";
# read file names in that directory into @pdfs
my @pdfs = grep /\.pdf\z/i, readdir DIR or die "Unable to read current
dir:$!\n";
my %DIR_LIST = map { $_ => 1 } @pdfs;
closedir DIR;
# open all the .TXT file from the 400
my $orders_dir = '/home/bvolk/test';
opendir ORDERS, $orders_dir or die "Can't open $orders_dir: $!";
# read all the item numbers in all the .txt files w/ map and
# load @ARGV for <> operator
@ARGV = map "$orders_dir/$_", grep !/^\./, readdir ORDERS;
my ( $job, $truck );
while ( <> ) {
( $job ) = $ARGV =~ /(\d+)/ if $. == 1;
$truck = $1 if /\A\$(\d+)/;
# $job and $truck are working great!!
# next unless the next line ends w/ .pdf Correct?
next unless /\A((.+)\.pdf)\z/;
my ( $key, $msds ) = ( $1, $2 );
# nothing below here is working. Should at least print the else statement
Right?
if ( exists $DIR_LIST{ $key } ) {
my $indy = 10;
print "Job $job printing msds $msds to $indy on truck # $truck\n";
}
else {
print "Job $job missing msds $msds\n";
}
}
Thank you again for your help!
Brian
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>