Re: RE: if else question





----- Original Message -----
From: Brian Volk <BVolk@xxxxxxxxxxxxxx>
Date: Thursday, April 28, 2005 12:45 pm
Subject: RE: if else question

>
> > > Hi All,
> > Hello,
> >
> > >
> > > The first time I wrote this if else statement I wrote it
> > > correctly... now
> > > I've confused myself... :~)
> > That will happen often !
> > >
> > > If the "if" statement returns false the program writes that
> line
> > > in the file
> > > to $error_log for every $file in the directory.... I just
> what
> > > the line in
> > > the file to be written one time... not one time for every
> > file in the
> > > directory... Perl is doing exactly what I have written...
> It's
> > > just not
> > > what I want.. :~)
> > >
> > > foreach line in the txtfile, match a file in the
> > > directory....print file.
> > > ...else print the line item in txtfile to the error_log....
> > >
> > > How do I write the code so the line item only prints to the
> > > error_log one
> > > time?
> > >
> > >
> > > my $file_dir = "F:";
> > > opendir(DIR, $file_dir) or die "Can't open the $file_dir: $!\n";
> > >
> > > my @files = readdir(DIR) or die "Unable to read current dir:$!\n";
> > > closedir(DIR);
> > >
> > > my $orders_dir = "c:/orders";
> > > opendir (ORDERS, $orders_dir) or die "Can't open $orders_dir: $!";
> > >
> > > @ARGV = map { "$orders_dir/$_" } grep { !/^\./ } readdir ORDERS;
> > >
> > > while (<>) {
> > > chomp;
> > >
> > > foreach my $file (@files) {
> > > my $error_log = "c:/brian/test/no_file.txt";
> > > open (NOFILE, ">> $error_log") or die "can't open
> $error_log:$!\n";> >
> > > if ($_ eq $file) {
> > > print $file;
> > >
> > >
> > > #### This is printing $_ to the error_log one time for every
> file
> > > in the
> > > directory
> > > #### I just want it to print to the error_log one time if
> there is
> > > not a
> > > match in the directory.
> > I am not exactly sure what you want, but it sounds like you
> > want exact oposite of what you already have ?? in which case,
> > the following should do:
> >
> > if ($_ ne $file) { print $file;}
> >
> > HTH,
> > Mark G.
>
> Mark, thank you for you help but actually the statement below is
> the one I'm
> having trouble getting correct. If the line item in the file
> does not
> match a filename in the directoy, print the line item to the
> error_log one
> time... right now it's printing the line item to the error_log,
> "NOFILE" one
> time for every file in the directory... So if I have a file w/ two
> lineitems (file names) that don't match a any of the files in the
> directoy, the
> error_log will have 50 entries for line item one and 50 entries
> for line
> item two..
OK I get it now. I have made a few changes to your original, particular because I don't liek to use @ARGV with in a script. I also changes the way you store file, moving it into a hash structure so comparisons are much faster. Feel free to modifie as is.

HTH,
Mark G.

#!PERL

use warnings;
use strict;

open RD,"te.txt" or die "ERROR: $!\n";
opendir RD_DIR, "." or die "ERROR: $!\n";;

my @TMP = readdir RD_DIR or die "ERROR: $!\n";
my %DIR_LIST;

$DIR_LIST{$_}=1 for @TMP;


foreach my $file ( <RD> ){
chomp $file;

unless( $DIR_LIST{$file} ){print STDERR "$file: NOFILE\n";next;}

# do stuff here if matched



}


>
> >
> > >
> > > } else {
> > > print NOFILE "\n";
> > > }
> > > }
> > >
> > > closedir (ORDERS);
> > >
> > >
>
> Thanks again!
>
> Brian
>
> >
> >
> >
>
> --
> To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
> For additional commands, e-mail: beginners-help@xxxxxxxx
> <http://learn.perl.org/> <http://learn.perl.org/first-response>
>
>
>

.



Relevant Pages