The file that the code above is parsing: test.wf#!/opt/perl-5.8.8/bin/perl -w # # # use strict; use Getopt::Long; use threads; use threads::shared; use Thread::Queue; require Data::Dumper; my $max_thread_count = 10; my $version = "0.1"; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time +); sub timestamp { my $time; $time = sprintf("%04d-%02d-%02d %02d:%02d:%02d",1900 + $year, $mon ++1, $mday, $hour, $min, $sec); return($time); } my $date = $mday . $mon+1 . 1900+$year; my $promoName; my $storeList; my $workFile; my $logFile; my $ocr; my $userid; my $store; my @stores; #my @sstores = sort map { sprintf 'store%03d', $_ } 0..$max_thread_cou +nt*2; my @sstores; my $parmStores; my @failedstores = (); share(@failedstores); my $thread; my $thread_list; my $tid; GetOptions( "promoname=s" => \$promoName, # promo name fo +r logging purposes "sl=s" => \$storeList, # Store List "wf=s" => \$workFile, # Work file "ocr=s" => \$ocr, # ocr "userid=s" => \$userid, # userid "store=s" => \$parmStores, # individual store +s if required ); if (!$promoName || !$workFile || !$userid) { printUsage(); exit 1; } if (! $storeList && ! $parmStores ){ print "Please indicate what you want to use: --sl for storelist fil +es or --store for individual stores\n"; exit 1; } if ($parmStores){ @stores = split(/,/, $parmStores); } if ($storeList){ if (! -f $storeList){ print "Error: $storeList not found\n"; exit 1; } } if (! -f $workFile){ print "Error: $workFile not found\n"; exit 1; } my $logFolder = "log/$promoName"; if (! -d $logFolder){ my $cmd = system("mkdir $logFolder"); } $logFile = "$logFolder/$promoName.$date.$userid.log"; sub writeLog { open(LOGFILE, ">>$logFile"); print LOGFILE ×tamp() . " $_[0]\n"; } sub failedStore { my $val = shift; push(@failedstores,$val); } # usage instructions sub printUsage { print "sendpromo v$version\n"; print "USAGE: $0 --promoname=promo name (for logging) - REQUIRED --sl=storelist files (can be seperated by commas) - REQUIRED --store=<stores comma seperated> (example: 001, 002 etc) --wf=work file - REQUIRED --ocr=xxxx (example 1234) --userid=userid - REQUIRED \n"; } # read store list(s) my $buffer; my $num_lines; sub readSL { my @list = split(/,/, $storeList); foreach my $storefile (@list) { print "opening $storefile\n"; open(STORELIST, "<$storefile"); @stores = <STORELIST>; chomp @stores; @stores = sort @stores; } } sub convertStores { foreach my $store(@stores){ $store = "s" . $store; push(@sstores, $store); } } print "------------------------------------------\n"; print "sendpromo v$version\n"; if ($ocr){ print "OCR #: $ocr\n"; } print "------------------------------------------\n"; writeLog("Starting for $promoName initiated by $userid"); unless($parmStores){ readSL(); } #convertStores(); print "stores: @stores\n"; #print "sstores: @sstores\n"; my $jobs = Thread::Queue->new(@stores); print "jobs: $jobs\n"; # read work file sub processWF { my $val = shift; open(WORKFILE, "<$workFile"); my @lines = <WORKFILE>; chomp @lines; while (defined (my $item = $jobs->dequeue)) { print "prog: running for $item\n"; writeLog("Creating thread for: $item"); foreach my $line (@lines) { $line =~ s/storeplaceholder/$item/g; if ($ocr) { $line =~ s/dateplaceholder/$date/g; $line =~ s/ocrplaceholder/$ocr/g; } print "Running: $line\n"; print "system: running $line for $item\n"; if (system($line) != 0){ writeLog("ERROR: Could not process $item : $line: $!"); failedStore($item); } } } close(WORKFILE); } $jobs->enqueue(undef) for 1..$max_thread_count; my @workers = map { threads->create( \&processWF ) } 1..$max_thread_co +unt; $_->join() for @workers; if (@failedstores){ my %fshash = map { $_ => 1 } @failedstores; @failedstores = sort keys %fshash; open(FS, ">stores.failed"); foreach my $fs (@failedstores){ $fs = substr $fs, 1; print FS $fs . "\n"; } close FS; print "\n\nstores.failed created ... please re-run sendpromo agains +t this when the stores are available\n"; writeLog("stores.failed created. Please re-run sendpromo against th +is when the stores are available\n"); } close(LOGFILE); print "--------------------------------------------------------------- +-----------------\n\n"; print "sendpromo complete.....Please check $logFile for additional inf +ormation....\n\n"; print "--------------------------------------------------------------- +-----------------\n"; 0;
In reply to Re^14: Help needed with regard to arrays
by theknightsofni
in thread Help needed with regard to arrays
by theknightsofni
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |