Ask a Question related to PERL Beginners, Design and Development.
-
Lonewolf #1
bad code, needs work...
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...
<-- 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;
$content_type@_\n
<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: $!";
open OUTFILE1, ">/home/multifax/pack-fishbait" or die "Can't open
${infile}.out at home: $!";
open OUTFILE2, ">/home/multifax/pack-food" or die "Can't open
${infile}.out at home: $!";
open OUTFILE3, ">/home/multifax/pack-ice_cream" or die "Can't open
${infile}.out at home: $!";
open OUTFILE4, ">/home/multifax/pack-other" or die "Can't open
${infile}.out at home: $!";
open OUTFILE5, ">/home/multifax/auto" or die "Can't open ${infile}.out at
home: $!";
open OUTFILE6, ">/home/multifax/barsupplies" or die "Can't open
${infile}.out at home: $!";
open OUTFILE7, ">/home/multifax/building" or die "Can't open ${infile}.out
at home: $!";
open OUTFILE8, ">/home/multifax/candy" or die "Can't open ${infile}.out at
home: $!";
open OUTFILE9, ">/home/multifax/candy2" or die "Can't open ${infile}.out
at home: $!";
open OUTFILE10, ">/home/multifax/caterer" or die "Can't open ${infile}.out
at home: $!";
open OUTFILE11, ">/home/multifax/pack-misc" or die "Can't open
${infile}.out at home: $!";
open OUTFILE12, ">/home/multifax/coffee" or die "Can't open ${infile}.out
at home: $!";
open OUTFILE13, ">/home/multifax/concessions" or die "Can't open
${infile}.out at home: $!";
open OUTFILE14, ">/home/multifax/dairy" or die "Can't open ${infile}.out
at home: $!";
open OUTFILE15, ">/home/multifax/convenience" or die "Can't open
${infile}.out at home: $!";
open OUTFILE16, ">/home/multifax/fruit_produce" or die "Can't open
${infile}.out at home: $!";
open OUTFILE17, ">/home/multifax/hotel" or die "Can't open ${infile}.out
at home: $!";
open OUTFILE18, ">/home/multifax/instit_regional" or die "Can't open
${infile}.out at home: $!";
open OUTFILE19, ">/home/multifax/instit_indep" or die "Can't open
${infile}.out at home: $!";
open OUTFILE20, ">/home/multifax/instit_ethnic" or die "Can't open
${infile}.out at home: $!";
open OUTFILE21, ">/home/multifax/indust" or die "Can't open ${infile}.out
at home: $!";
open OUTFILE22, ">/home/multifax/janitorial" or die "Can't open
${infile}.out at home: $!";
open OUTFILE23, ">/home/multifax/jan_cleaning" or die "Can't open
${infile}.out at home: $!";
open OUTFILE24, ">/home/multifax/office" or die "Can't open ${infile}.out
at home: $!";
open OUTFILE25, ">/home/multifax/paper_multi" or die "Can't open
${infile}.out at home: $!";
open OUTFILE26, ">/home/multifax/paper_food" or die "Can't open
${infile}.out at home: $!";
open OUTFILE27, ">/home/multifax/paper_indep" or die "Can't open
${infile}.out at home: $!";
open OUTFILE28, ">/home/multifax/restaurant_supply" or die "Can't open
${infile}.out at home: $!";
open OUTFILE29, ">/home/multifax/vending-regional" or die "Can't open
${infile}.out at home: $!";
open OUTFILE30, ">/home/multifax/vending-indep" or die "Can't open
${infile}.out at home: $!";
open OUTFILE31, ">/home/multifax/all_other" or die "Can't open
${infile}.out at home: $!";
open OUTFILE32, ">/home/multifax/unclassified" or die "Can't open
${infile}.out at home: $!";
open OUTFILE33, ">/home/multifax/cash_grocer" or die "Can't open
${infile}.out at home: $!";
open OUTFILE34, ">/home/multifax/cash_thomas-howard-green" or die "Can't
open ${infile}.out at home: $!";
open OUTFILE35, ">/home/multifax/cash_thomas-howard-spartan" or die "Can't
open ${infile}.out at home: $!";
open OUTFILE36, ">/home/multifax/grocery" or die "Can't open ${infile}.out
at home: $!";
open OUTFILE37, ">/home/multifax/hardware" or die "Can't open
${infile}.out at home: $!";
open OUTFILE38, ">/home/multifax/rental" or die "Can't open ${infile}.out
at home: $!";
open OUTFILE39, ">/home/multifax/paper_party-indep" or die "Can't open
${infile}.out at home: $!";
open OUTFILE40, ">/home/multifax/paper_party_store" or die "Can't open
${infile}.out at home: $!";
open OUTFILE41, ">/home/multifax/empty_sic" or die "Can't open
${infile}.out at home: $!";
open OUTFILE42, ">/home/web/sales/cust-parsed" or die "Can't open
${infile}.out at home: $!";
while (<INFILE>) {
$rec++;
$i++;
chomp;
@fields = split /\s*\|\s*/, $_;
$fields[0] =~ s/^\s+//;
$fields[1] =~ s/ /_/g;
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.
# $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 "$fields[1]\@$tmptxt\n"; #your trailing ID
#* *print OUTFILE42 "$_|" foreach (@fields);
if ($fields[11] == 102){
print OUTFILE1 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 104){
print OUTFILE2 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 106){
print OUTFILE3 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 108){
print OUTFILE4 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 109){
print OUTFILE5 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 110){
print OUTFILE6 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 112){
print OUTFILE7 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 113){
print OUTFILE8 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 114){
print OUTFILE9 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 116){
print OUTFILE10 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 118){
print OUTFILE11 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 119){
print OUTFILE12 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 120){
print OUTFILE13 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 122){
print OUTFILE14 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 123){
print OUTFILE15 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 124){
print OUTFILE16 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 127){
print OUTFILE17 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 129){
print OUTFILE18 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 130){
print OUTFILE19 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 131){
print OUTFILE20 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 132){
print OUTFILE21 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 133){
print OUTFILE22 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 134){
print OUTFILE23 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 139){
print OUTFILE24 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 149){
print OUTFILE25 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 151){
print OUTFILE26 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 153){
print OUTFILE27 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 161){
print OUTFILE28 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 170){
print OUTFILE29 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 173){
print OUTFILE30 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 176){
print OUTFILE31 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 177){
print OUTFILE32 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 182){
print OUTFILE33 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 183){
print OUTFILE34 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 185){
print OUTFILE35 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 187){
print OUTFILE36 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 189){
print OUTFILE37 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 191){
print OUTFILE38 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 193){
print OUTFILE39 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
elsif ($fields[11] == 196){
print OUTFILE40 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
else {
print OUTFILE41 "$fields[1]\@$tmptxt\n"; #your trailing ID
}
}
close INFILE;
close OUTFILE;
close OUTFILE1;
close OUTFILE2;
close OUTFILE3;
close OUTFILE4;
close OUTFILE5;
close OUTFILE6;
close OUTFILE7;
close OUTFILE8;
close OUTFILE9;
close OUTFILE10;
close OUTFILE11;
close OUTFILE12;
close OUTFILE13;
close OUTFILE14;
close OUTFILE15;
close OUTFILE16;
close OUTFILE17;
close OUTFILE18;
close OUTFILE19;
close OUTFILE20;
close OUTFILE21;
close OUTFILE22;
close OUTFILE23;
close OUTFILE24;
close OUTFILE25;
close OUTFILE26;
close OUTFILE27;
close OUTFILE28;
close OUTFILE29;
close OUTFILE30;
close OUTFILE31;
close OUTFILE32;
close OUTFILE33;
close OUTFILE34;
close OUTFILE35;
close OUTFILE36;
close OUTFILE37;
close OUTFILE38;
close OUTFILE39;
close OUTFILE40;
close OUTFILE41;
close OUTFILE42;
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;
}
Lonewolf Guest
-
PHP Command/Code to work out where its being accessed/run from?
Hi, Is there a simple way to work out where the code is being run from? For example, you goto www.mydomain5.co.uk, when the index.php is run,... -
Can't get a code group to work
I'm trying to use .Net Code Access Security. I've tried to create a code group at the user level (using strong name membership). Every time I run... -
radio button don't work with code behind
radio buttons give error messages when programming using code-behind If you guys can help you will save my life help help help. following is the... -
Code view doesn't work - DW4 Mac
For some reaason I can't get into code view. Neither the "Show Code View" button or the menu command View > Code are working. Is there any way to... -
PHP code won't work,easy
"Star2" <sarg@armydogtags.com> wrote in message news:3F09FDF1.7E0666DA@armydogtags.com... back to 50 etc I'm still here and there's no need to... -
Wiggins D Anconia #2
Re: bad code, needs work...
Looping is your friend :-)...
have> 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 toFor this specific question you may want to see the other threads about> a "1-" added to the front of that field...
>
Jan's (I think) function dealing with checking whether a value is in a
list of other values.
${infile}.out> <-- 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;
> $content_type@_\n
> <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<snip outfile opens>> at home: $!";
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.
In the below test you could create an array once of the cities that>
>
> while (<INFILE>) {
> $rec++;
> $i++;
> chomp;
> @fields = split /\s*\|\s*/, $_;
> $fields[0] =~ s/^\s+//;
> $fields[1] =~ s/ /_/g;
>
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.
Huh?> 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.
>
unless> # $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<snip list of prints>> $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 "$fields[1]\@$tmptxt\n"; #your trailing ID
> # print OUTFILE42 "$_|" foreach (@fields);
>
> if ($fields[11] == 102){
> print OUTFILE1 "$fields[1]\@$tmptxt\n"; #your trailing ID
> }
> elsif ($fields[11] == 104){
> print OUTFILE2 "$fields[1]\@$tmptxt\n"; #your trailing ID
> }
>
Again if you store the output handles to a hash, then you can look up> else {
> print OUTFILE41 "$fields[1]\@$tmptxt\n"; #your trailing ID
> }
>
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...
<snip closes>>
> }
> close INFILE;
> close OUTFILE;
Again, if your handles are in a hash, just loop over the hash calling
close on each handle rather than hard coding them all...
If you run into code where virtually the same code is copy and pasted> 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;
> }
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...
[url]http://danconia.org[/url]
Wiggins D Anconia Guest
-
Wiggins D Anconia #3
Re: bad code, needs work...
>
> 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];
> > }
p.s. regardless of how you handle it, the above test needs to be done
with 'eq' rather than '=='. == is the number equality test, 'eq' is the
string equality test, which is what you are testing... same goes for in
the 'grep' I provided...
[url]http://danconia.org[/url]
Wiggins D Anconia Guest
-
R. Joseph Newton #4
Re: bad code, needs work...
LoneWolf wrote:
I think the problem comes up right here. You are dscribing the problem in terms> 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...
of
details of the mechanism you think may work, rather than in terms of its logical
description.
Doing this handicaps your effort from the start. "One field"? Of what? What
is it about
that field that makes for a match?
This should be at least three sentences. Not a Perl issue, you say? Think>
>
> <-- 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;
> $content_type@_\n
> <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>
again. The above is
meaningful only to someone who doesn't need to read it. All the user seeing
this from the web
should need to know is the end result. The details are your [the programmers]
problem.
Just as the above should be broken down into clear sentences, so should the
problem itself.
Please see Wiggins post concerning the code itself. Most of this should be done
with loops,
and probably a small data file to hold the names of all the files you are
seeking to write.
Can you try re-posing the problem de novo? Instead of describing programming
structures,
describe what you have for information coming in, and what you hope to come out
with?
You get there much faster when you know where you are headed.
Joseph
R. Joseph Newton Guest



Reply With Quote

