Page 2 of 2 First 12
  • Jump to page:
    #16
  1. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,932
    Rep Power
    1225
    Here's an example how to load your items.txt into a hash as well as the primary portion of parsing the exports.txt file. The parsing of the fields of the exports file will probably need a slight adjustment because your description of which fields you need is not clear enough to figure out what you want.

    Code:
    #!/usr/bin/perl
    
    use 5.10.0;
    use strict;
    use warnings;
    use Data::Dumper;
    
    my %items;
    
    open my $items_fh, '<', 'items.txt' or die "failed to open items.txt <$!>";
    while (my $line = <$items_fh>) {
        chomp $line;
        my ($item, $qty, $desc, $pickloc, $bpc) = split(/\s\s+/, $line);
        $item =~ s/^0+//;
        $qty =~ tr/[]//d;
        $items{$item} = {
            quantity => $qty,
            name     => $desc,
            location => $pickloc,
            amount   => $bpc,
            
        };
    }
    say "Let's see what's in the \%items hash:";
    say Dumper \%items;
    
    say "Let's parse and dump the desired fields from exports.txt";
    open my $export_fh, '<', 'export.txt' or die "failed to open export.txt <$!>";
    while (my $line = <$export_fh>) {
        my ($location, $item, $material, $stock, $grdate) = (split(/\|/, $line))[2,7,8,9,14];
        tr / //d for ($location, $item, $material, $stock, $grdate);
        tr /,//d for ($material, $stock);
        say Dumper ($location, $item, $material, $stock, $grdate);
        
        next unless exists $items{$item};
        
        # process the data as needed
    }
    Based on your sample data, this is what it outputs.
    Code:
    Let's see what's in the %items hash:
    $VAR1 = {
              '20339' => {
                           'amount' => '12',
                           'location' => 'CAA011A',
                           'name' => 'AAABatteries',
                           'quantity' => '12'
                         },
              '9138655' => {
                             'amount' => '24',
                             'location' => 'CAA011A',
                             'name' => 'Trinkets24perpack',
                             'quantity' => '24'
                           },
              '9010000' => {
                             'amount' => '4',
                             'location' => 'CAA011A',
                             'name' => 'Large Easter Eggs',
                             'quantity' => '4'
                           },
              '9031484' => {
                             'amount' => '24',
                             'location' => 'CAA011A',
                             'name' => 'HolidayTrinkets',
                             'quantity' => '24'
                           },
              '1222827' => {
                             'amount' => '12',
                             'location' => 'CDD214G',
                             'name' => 'GIFT PAXX',
                             'quantity' => '126'
                           }
            };
    
    Let's parse and dump the desired fields from exports.txt
    $VAR1 = 'CAA011A';
    $VAR2 = '20339';
    $VAR3 = '444';
    $VAR4 = '444';
    $VAR5 = '08/01/2012';
    
    $VAR1 = 'CAA011A';
    $VAR2 = '9010000';
    $VAR3 = '268';
    $VAR4 = '268';
    $VAR5 = '08/29/2012';
    
    $VAR1 = 'CAA011A';
    $VAR2 = '9031484';
    $VAR3 = '216';
    $VAR4 = '216';
    $VAR5 = '09/21/2012';
    
    $VAR1 = 'CAA011A';
    $VAR2 = '9103893';
    $VAR3 = '318';
    $VAR4 = '318';
    $VAR5 = '12/05/2011';
    
    $VAR1 = 'CAA011A';
    $VAR2 = '9138655';
    $VAR3 = '1104';
    $VAR4 = '1104';
    $VAR5 = '08/01/2012';
    
    $VAR1 = 'P24591A';
    $VAR2 = '20339';
    $VAR3 = '660';
    $VAR4 = '660';
    $VAR5 = '09/14/2012';
  2. #17
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0
    I really appreciate your reply. I will walk through it next weekend. I left my full flat files at work.

    I ended up making it two ways, one where I cycle through like above and one where I wrote it into a hash but I did not see a performance loss/gain.
    It took about ten minutes either way. Admittedly, this might be because of how I built the hashes.

    No matter what, I still have to check each item number in items.txt to match all the lines in export.txt

    Hopefully I can use your example to merge all like-items of export and output both hashes.

    The program is currently quite a bit longer and I am still working on it. It's working though and production increased about 20% for several guys. I will definitely look into your code soon and maybe even writing perl better.

    Edit: $bpc is bats per case. To be compared to export.txt, amount's amount must be multiplied by $bpc or divide export's qty by $bpc.

    I will have to do math on each location, to verify whether or not the condition of $amount is "< 0" AT ONE LOCATION (The first pick location, in the above example it is CAAxxxx) when
    '$exportQty / $bpc'
    $exportQty - $amount

    After that, if there's a negative, then I need to add it to the right column. I will post a longer working example of this when I get back to my work computer.
  4. #18
  5. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,932
    Rep Power
    1225
    If you post your updated code, we might be able to spot some areas that you missed where it could be improved/optimized.
  6. #19
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0
    Originally Posted by FishMonger
    If you post your updated code, we might be able to spot some areas that you missed where it could be improved/optimized.
    There's no might about it, haha.
    I edited my above post to include more info.

    Unfortunately, I left my flash drive locked up at work, with all the current .pls being used. I have a copy at home but both full flat files are also on my flash drive.
  8. #20
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0

    Red face


    I got it down to 2 minutes by rebuilding the export to only include the fields I need.

    I can see that I could do this without writing and then reading, but I was having issues with the array holding its shape. It was one continuous line and I don't know why. When I write/read it, it's fine?


    Code:
    #!/usr/bin/perl
    @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
    @days = qw(Sun Mon Tues Wed Thu Fri Sat);
    ($second, $minute, $hour, $dom, $month, $yo, $dow, $doy, $dst) = localtime();
    $year = 1900 + $yo;
    #$hour:$minute:$second - $days[$dow] $months[$month], $dom, $year
    $today = $days[$dow];
    if ($hour > 12){
    $hour = $hour - 12;
    }
    $ts = $months[$month] . "" . $dom . "_" . $hour . "_" . $minute;
    $stamp = $today . "_" . $ts;
    
    use File::Copy;
    $fi = "C:/export.txt";
    $modT = -M $fi;
    if (!-e $fi){ print "No export!! Exiting in 5 seconds\n"; sleep(5); exit; }
    if($modT < 0.020834){
    if($half eq ""){
    print "\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\n\#\# ____ COLLECT #\n\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\#\n";
    print "Collect 1st half or 2nd half Case items - (1 or 2): ";
    $half = <>;
    chomp($half);
    }
    if ($half == 87) { $tag = "ALL"; $dlc = " <> 9"; print "Collecting ALL!\n"; }
    if ($half == 1) { $tag = "CasesPt1"; $dlc = "='1'"; print "Collecting 1st half"; }
    if ($half == 2) { $tag = "CasesPt2"; $dlc = "='2'"; print "Collecting 2nd half"; }
    if ($half == 3) { $tag = "CasesPt3"; $dlc = "='3'"; print "Collecting 3rd part"; }
    if (!$dlc){ print "\nExiting.\n"; exit; }
    print " @ " . $hour . "_" . $minute . "\n";
    use DBI;
    $afeni = chr(115) . chr(97);
    $tupac =  chr(111) . "o" . chr(111) . chr(111) . chr(111);
    $dbs = "dbi:ODBC:DRIVER={SQL Server}; database={xxx}; SERVER={10.10.10.10}";
    $dbh = DBI->connect($dbs, $afeni, $tupac) or die "$DBI:errstr \n";
    $sth = $dbh->prepare("SELECT skutext AS 'Item', quotename(Sum(Quantity),'') AS Amount, description AS Description, PickLocation AS Location, BPC AS bpc FROM Items WHERE ShippingDC='__' AND (Unit='C' OR Unit='K') AND DownloadNumber$dlc AND (PickLocation LIKE '%%CAA%%' OR picklocation like '%%CEE%%' OR picklocation like '%%CDD%%' OR picklocation like '%%WGG%%' OR picklocation like 'K%%') GROUP BY skutext, Picklocation, BPC, Description ORDER BY PickLocation") or die "Failed SQL\n" . $dbh->errstr;
    
    $sth->execute();
    open(ITEMP,">items.txt");
    while (@data = $sth->fetchrow()){
    	# Strip 4
    	if($data[3] =~ /WGG02/i){ next; }
    	if($data[3] =~ /WGG04/i){ next; }
    	if($data[3] =~ /CDD10/i){ next; }
    	if($data[3] =~ /CDD12/i){ next; }
    		print ITEMP "$data[0]$data[1]$data[2]$data[3]$data[4]\n";
    	
    }
    close(ITEMP);
    $sth->finish;
    $dbh->disconnect;
    open(IMP, "<items.txt");
    @dat = <IMP>;
    close(IMP);
    print "Rebuilding Info \n";
    open(EXP,"<C:/export.txt");
    @sapr = <EXP>;
    close(EXP);
    @revs = reverse(@sapr);
    foreach $line (@revs){
    if ($line =~ /WGG/i){
    	push(@news, $line);}
    }
    foreach $line (@sapr){
    	if ($line =~ /WGG/i){ next; } 
    	if ($line =~ /RETURNS/i){ next; }
    	if ($line =~ /CONVEYOR/i){ next; }
    	if ($line =~ /BREAK/i){ next; }
    	if ($line =~ /UNSALEABLE/i){ next; }
    	else { push(@build, $line); }
    }
    
    @sap = "@news" . "@build";
    
    open(DOR,">export_mod.txt");
    print DOR @sap;
    close(DOR);
    open(EXP, "<export_mod.txt");
    @sap = <EXP>;
    close(EXP);
    
    foreach $linea (@sap) {
    $linea =~ s/\s//gi;
    $linea =~ s/\s+//gi;
    ($un0, $type, $sloc, $un1, $un2, $type2, $un3, $sitn, $qty1, $qty2, $ea, $un4, $un5, $un6, $grdate, $un7, $un8, $un9,$un18,$un19,$un20,$un21) = split(/\|/, $linea);
    $qty1 =~ s/,//;
    $qty1 =~ s/(.*)-/-$1/;
    push (@resarr, "$sloc|$sitn|$qty1|$grdate");
    }
    
    open(FILE,">daily_new.txt");
    print "Generating list ... \n";
    foreach $line (@dat) {
    $line =~ s/\]/\]\ \ \ /gi;
    $line =~ s/\[//gi;
    $line =~ s/\]//gi;
    $line =~ s/\s\s\s+/\ \ \ /gi;
    ($itn, $cgo, $desc, $pickloc, $bpc) = split(/   /, $line);
    	foreach $entry (@resarr) {
    	($sloc, $sitn, $qty1, $grdate) = split(/\|/, $entry);
    		$entry =~ s/<<empty>>/0/;
    		if ($sitn == $itn){
    			$qty1 =~ s/\s//;
    			$qty1 =~ s/(.*)-/-$1/;
    			$qty1 =~ s/,//;
    			$qty1 = $qty1 / $bpc;
    			$sloc =~ s/\s//;
    			 if($sloc =~ /$pickloc/) {
    				$need = $qty1-$cgo;
    				$qoh = $qty1;
    				if($need < 0){
    					$need =~ s/-//;
    					print FILE "\n($itn) [$pickloc] [$desc] Going Out:" . sprintf("%.2f", $cgo) . " | On-Hand: " . sprintf("%.2f", $qoh) . " | Need: " . sprintf("%.2f", $need) . "\n";
    				}
    				next;
    			}
    			if($qoh-$cgo < 0){
    				if($sloc =~ /^\D\D\D\D/){
    					next;
    				}
    			$flag = "QOH=$qoh";
    			print FILE "\t\t\t\t\t\t $sloc - $grdate - (" . sprintf("%.2f",$qty1) . ") \n"; 
    			}
    
    		}
    	}
    }
    close(FILE);
    copy("daily_new.txt", "C:/stuff$tag$stamp.txt");
    print "Copying file to Shared {Folder} $tag$stamp.txt ... \n";
    print "Success. Exiting ... \n";
    sleep(3);
    } else {
    print "export.txt is more than 30 minutes old. Exiting \n";
    sleep(7);
    }
    I just realized I don't have the export or the items list on my flash drive, and I'm sure I put them on there.

    They're still structured the same way as previously mentioned on page 1. There's just a lot more of them.

    I have it reverse the array because the first match of $pickloc to $sloc will be the pickLocation, and this is where I build the each list's "header".

    I have 2 visual basic projects right now and this might get put off for another week or so. 2 minutes is a big jump from 15, then 10, then 7... still not pro yet

    Thanks again for any advice, I will study it and make use of it in the future.
  10. #21
  11. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,932
    Rep Power
    1225
    Good to hear that you're satisfied with that script. I'm even more glad that I don't need to maintain that mess.

    Why didn't you incorporate any of the suggestions that we made?
  12. #22
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    15
    Rep Power
    0
    Originally Posted by FishMonger
    Good to hear that you're satisfied with that script. I'm even more glad that I don't need to maintain that mess.

    Why didn't you incorporate any of the suggestions that we made?
    Only a lack of time lately, not a lack of desire. It's not wise to take a limited time frame and throw several unknown techniques (to me) into my programming. Like I said, I plan to study what's been shared and go back in there and do it a cleaner way. I am still open to more suggestions to study and implement. This program needs a lot of work until it's finished and I enjoy working on it, I am just missing time right now.

    The vb is taking priority now because it's for the bosses.
Page 2 of 2 First 12
  • Jump to page:

IMN logo majestic logo threadwatch logo seochat tools logo