On Sun, 10 Oct 2004, James wrote: > I am writing a program to read from an input file and > verify or not if the Quantity equal to the number of > parts. The problem is some of the parts can be in 2 or > 3 lines. Fromthe data file, the quantity match the > number of parts but I cannot find the logic to write a > program. Please help
The three problems with the original script were that: (1) @pts was never re-initialized, so it just kept growing, (2) the comparison between $qty and @pts can't be done until you know you've processed all of the CONT lines for an item, and (3) the number of items in @pts is length(@pts), or scalar(@pts), or 1+$#pts, but not $#pts. Here's a modified copy of the script that addresses those issues: [...] while (<DATA>) { chomp; next if /^\s*ITEM/; if ( m!^\s*\d+! ) { &check_qty($item,$qty,@pts) if $item; ($item, $qty, $part) = split ' ', $_, 3; @pts = split(',',$part); } elsif ( s!^\s+CONT\s*!! ) { push @pts, split(',',$_); } } &check_qty($item,$qty,@pts); sub check_qty { my($item,$qty,@pts) = @_; if ( $qty != 1+$#pts ) { printf "Quantity does not match parts for item $item\n"; } } [...] > > use strict; > use warnings; > > > my ($item,$qty,$part,$part2); > my @pts; > > while (<DATA>) > { > > chomp; > next if /^\s*ITEM/; > > if ( m!^\s*\d+! ) > { > ($item, $qty, $part) = split ' ', $_, 3; > foreach ( split /,/, $part ) > { push ( @pts, $_ ); } > } > elsif ( m!^\s+CONT! ) > { > ( undef, $part2) = split ' ', $_, 2; > foreach ( split /,/, $part2 ) > { push ( @pts, $_ ); } > } > > if ( $qty != $#pts ) > { > print "Quantity does not match parts\n"; > } > } > > __DATA__ > ITEM QTY PARTS > 1 1 P3 > 2 1 MP4 > 3 4 J15,MP6,RC1,MP8, > 4 4 MP9,RC19,MP11,MP1, > 5 15 P1,P2,P15,P16,P17, > CONT AP18,MP19,MP20,MP21,MP42, > CONT BP23,MP24,MP25,MP26,MP27, > 6 8 MP28,MP29,MP30,MP31,MP32, > CONT AR3,AR34,MP35, > 7 2 PS6,PS5 > > > > > __________________________________ > Do you Yahoo!? > Take Yahoo! Mail with you! Get it on your mobile phone. > http://mobile.yahoo.com/maildemo > _______________________________________________ > Perl-Win32-Users mailing list > [EMAIL PROTECTED] > To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs > -- ------------------------------------------------------------------------- Tom Pollard [EMAIL PROTECTED] Schrodinger, Inc. 646-366-9555 x102 ------------------------------------------------------------------------- _______________________________________________ Perl-Win32-Users mailing list [EMAIL PROTECTED] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs