I think reading the files into a hash would help here.
Try this code:
This code prints:
Old inventory for a is 10:10
Old inventory for b is 10:10
Old inventory for c is 15:07
Old inventory for d is 12:10
new inventory for a is 10:10:09
new inventory for b is 10:10:05
new inventory for c is 15:07:07
new inventory for d is 12:10:10
new inventory for e is 00:00:50
new inventory for f is 00:00:62
------------------
use strict;
my (%old, %new, %today);
my ($item, $lastInv, $blank);
my %allItems;
%old = (
a => "10:10",
b => "10:10",
c => "15:07",
d => "12:10",
);
%today = (
a => "09",
b => "05",
d => "10",
e => "50",
f => "62",
);
%new=%old;
#Get all of the items
foreach $item (keys(%today)){
$allItems{$item}=1;
}
foreach $item (keys(%old)){
$allItems{$item}=1;
$blank=$old{$item}; #Need to make a "blank" item
}
$blank =~ s/\d/0/g;
foreach $item (keys(%allItems)){
if (exists($old{$item}) && exists($today{$item})) {
#have the item in old and today. Need to append to end of old
$new{$item} = $new{$item} . ":$today{$item}";
#print "The old string is $old{$item} and today I have
$today{$item}\n";
#print "The new string is $new{$item}\n";
}elsif(exists $old{$item} && ! exists($today{$item}) ){
#Have the item in old, but not today. need to duplicate last value of
old
$lastInv = substr($old{$item}, -2);
$new{$item} = $new{$item} . ":$lastInv";
#print "The old string is $old{$item} and last inventory for $item is:
$lastInv\n";
#print "The new string is $old{$item}\n";
}else{
#This is a brand new item. Need to create a new entry padded with 0.
#print "I have a brand new item $item\n";
$new{$item} = $blank . ":$today{$item}";
}
}
print "\n\n";
foreach $item (sort keys(%old)){
print "Old inventory for $item is $old{$item}\n";
}
print "\n\n";
foreach $item (sort keys(%new)){
print "new inventory for $item is $new{$item}\n";
}
___________________________________
Michael Prozinski
Interautomation, Inc.
email => [EMAIL PROTECTED]
Dearborn => (313) 390-3855
Mobile => (734) 216-6875
-----Original Message-----
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED]]On Behalf Of
Jean-Paul Felix
Sent: Thursday, May 10, 2001 9:01 AM
To: Perl-Win32-Users (E-mail)
Subject: RE: logic problems in while loop resend
Thanks for replies, I need to explain some more because
I think my code might be better ignored!
The files are inventory balances for each day.
I need to check todays file against the old file (yesterdays)
and:
Add deleted stuff to the new file (to which I will add
a balance of zero).
The new file will then become a revised version of todays.
The code I'm having difficulty writing is how to compare one
file with another by matching fields unique to both and
adding fields not matched. theoretical example:
old equals
a 10:10
b 10:10
c 15:10
d 12:10
today equals
a 09
b 05
d 10
e 50
f 50
new wants to be
a 10:10:09
b 10:10:05
d 12:10:10
e 00:00:50
f 00:00:50
c 15:10:00
Here's some of my code for the subroutine comparing
old against today. It is way off as it maintains it's
place in the today file.
I need it to check the whole of the NEW file each time and
if found:
exit the loop with variables containing data
from that line to add to new file.
if not found:
add $a1 (plus $a2,3,etc) to new file.
Please note that there are other substitutions made $a2, $a3, etc,
which are not part of the if but will need to be available to
add to NEW.
patience and observations appreciated.
jpf
while (<OLD>) {
# read in old file line at a time
chomp;
for ($a1 = substr($_,1,12)) {s/^\s+//;s/\s+$//}
while (<TODAY>) {
# find line in new file
$found=0;
for ($b1 = substr($_,0,12)) {s/^\s+//;s/\s+$//}
if ($a1 eq $b1) {
$found=1;
print NEW "$a1\n";
last
}
if ($found eq 0) {print NEW "*** $b1\n";}
last
}
}
_______________________________________________
Perl-Win32-Users mailing list
[EMAIL PROTECTED]
http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
test.pl