Looping is your friend :-)...


> Here's what I have, and I am sure there is a better way to do this...
> 
> The problem I am getting is checking to see if one field matches the city,
> and if it does keeping it the same, however if it does not it needs to
have
> a "1-" added to the front of that field...
> 

For this specific question you may want to see the other threads about
Jan's (I think) function dealing with checking whether a value is in a
list of other values.

> <-- CODE -->
> #!/usr/bin/perl -w
> 
> #####
> # 
> # Main Functions of the script, makes calls to all of the subscripts
> performing the functions.
> # Also creates pretty output for everyone to check out.  This script is
> designed to run after WGEN has been run.
> #
> #####
> 
> no warnings qw(uninitialized);
> 
> $content_type   = "Content-Type: text/html\n\n";
> 
> print <<ENDPAGE;
> [EMAIL PROTECTED] 
> <html>
> <head>
>       <title>Customer Parsing Form</title>
> <body bgcolor="white" text="black"> 
> <BR>
> <center> CUSTOMER FILE DOWNLOAD AND CONFIGURE</center>
> <HR>
> <BR>
> This will download and create the files onto the Intranet from Stanpak. 
> Login to Stanpak FIRST and run ROBERT and ROBERT2 from *QICLOOK to create
> the files this script needs to handle the rest of this process.<BR>
> ENDPAGE
> print "Now getting the files from the server (salesa*)<BR><HR>";
> #&get_files;
> print "<BR><HR><BR><font +2>IF you have done the *QICLOOK report (ROBERTC)
> then the files will be there for the server to download.  Once those files
> are there and downloaded this script will get the files and put them into
> the flat files for inventory and *** items.</font><BR>";
> &cleanup;
> print <<ENDPAGE2;
> This script has finished running and ALL pages should now be updated.
> </body>
> </html>
> ENDPAGE2
> 
> 
> 
> sub cleanup{
> 
> use strict;
> use warnings;
> my (@fields, $lng);
> 
> opendir INDIR , "/home/web/sales/info/test" or die "Can't open dir with
> before files:$!";
> #$infile = custs1;
> 
> foreach my $infile (grep {!/^\./} readdir INDIR) {
> #read all the files in your home/sql dir
> #read only files that do not start with a .
>   my ($i,$rec, $tmptxt);
> 
>   open INFILE, "</home/web/sales/info/test/$infile" or die "Can't open
> $infile: $!";
>   open OUTFILE, ">/home/multifax/everyone" or die "Can't open
${infile}.out
> at home: $!";

<snip outfile opens>

The outfile opens can be handled in a loop that will at least prevent
the copying and pasting. You can store a FILEHANDLE to a lexical and
then store that scalar into a hash, which would be a convenient way to
store your output handles.  Alternatively you can use IO::File and go
all OOP... either way your OUTFILE opens can be handled in a loop.

> 
>   
>   while (<INFILE>) {
>    $rec++;
>    $i++;
>    chomp;
>    @fields = split /\s*\|\s*/, $_;
>    $fields[0] =~ s/^\s+//;
>    $fields[1] =~ s/ /_/g;
> 

In the below test you could create an array once of the cities that
match, then just grep for the value in that city list with something like,

if (grep $fields[4] == $_, @cities) {
  $tmptxt = $fields[10];
}
else {
  $tmptxt = '1-' . $fields[10];
}

No more copying and pasting, to add a city just add it to the array.

>    if ($fields[4] == "RALEIGH"){ 
>     $tmptxt = $fields[10];
>    }
>    elsif ($fields[4] == "CARY"){ 
>     $tmptxt = $fields[10];
>    }
>    elsif ($fields[4] == "DURHAM"){ 
>     $tmptxt = $fields[10];
>    }
>    elsif ($fields[4] == "APEX"){ 
>     $tmptxt = $fields[10];
>    }
>    elsif ($fields[4] == "MORRISVILLE"){ 
>     $tmptxt = $fields[10];
>    }
>    elsif ($fields[4] == "Holly Springs"){ 
>     $tmptxt = $fields[10];
>    }
>    elsif ($fields[4] == "FUQUAY-VARINA"){ 
>     $tmptxt = $fields[10];
>    }
>    elsif ($fields[4] == "FUQUAY VARINA"){ 
>     $tmptxt = $fields[10];
>    }
>    elsif ($fields[4] == "FUQUAY-VARINA"){ 
>     $tmptxt = $fields[10];
>    }
>    else {
>     $tmptxt = "1-$fields[10]";
> #    $tmptxt .= $fields[10];
>    }    
> 
>    #there is probably a way to get rid of the trailing spaces in the first
> entry using split,I just couldnt think of any.
> 

Huh?

> #   $lng = @fields unless $lng; #set $lng for first record
> #   print "The following record: $i has ", scalar @fields, " fields as
> compared to $lng fields in the first record!  Skip. : $_\n" and next
unless
> $lng == @fields;
> #   poor quality control of your input data: check if all reords have the
> same number of fields or skip and print record otherwise.
> #   i++;
> 
>    print OUTFILE42 "$_|" foreach (@fields);
>    print OUTFILE "[EMAIL PROTECTED]"; #your trailing ID
> #   print OUTFILE42 "$_|" foreach (@fields);
> 
>    if ($fields[11] == 102){ 
>     print OUTFILE1 "[EMAIL PROTECTED]"; #your trailing ID
>    }
>    elsif ($fields[11] == 104){ 
>     print OUTFILE2 "[EMAIL PROTECTED]"; #your trailing ID
>    }
> 
<snip list of prints>

>    else { 
>     print OUTFILE41 "[EMAIL PROTECTED]"; #your trailing ID
>    }
> 

Again if you store the output handles to a hash, then you can look up
which output handle you need to print to using the hash key. Then you
only need one print.  You will have to retrieve the filehandle into a
simple scalar to use plain print, alternatively if they are IO::File's
you can use the print method directly on the object reference still in
the hash.  Either way the copying and pasting has got to go...

> 
>   }
>   close INFILE;
>   close OUTFILE;

<snip closes>

Again, if your handles are in a hash, just loop over the hash calling
close on each handle rather than hard coding them all...

>   print "Read $rec records from /home/web/sales/info/test/$infile and
> printed $i into /home/web/sales/info/test/${infile}.out\n"; } closedir
> INDIR;
> }

If you run into code where virtually the same code is copy and pasted
lots of times then something is definitely amiss and you need to
re-examine whether an improved and possibly more complex data structure
can help you.  In the above it definitely can.

Two steps back, breathe, now come at the problem from a different angle...

http://danconia.org


-- 
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