fileA.txt contains what you listed as File A in the question. I put the content of File B into __DATA__.
#!/usr/local/bin/perl -w use strict; my(%products) = (); open(my $fh, '<', './fileA.txt') || die "Could not read fileA.txt: $!\ +n"; while(<$fh>){ chomp; my($key, $value)=split(/\|/); $products{$key} = $value; } close($fh); my(@groups)=(); { local $/; my $string = <DATA>; @groups = split(/(?=Group)/, $string); } foreach (@groups){ my $name; if(m/Group Name: (.+)/){ $name = $1; }else{ print STDERR "Could not find group name\n"; next; } my(@prods); if(m/Product Name:\n(.*)\Z/ms){ my $prod = $1; @prods = split(/\n/, $prod); }else{ print "$name\n"; print STDERR "Group $name didn't have any products\n"; next; } print "Group ".$name."\n"; my $total = 0; foreach (@prods){ print "$_ $products{$_}\n"; $total += $products{$_}; } print "Total ".join('+', @prods)." = $total\n"; } __DATA__ Group Name: Small Junk: lll Product Name: AAA BBB Group Name: Big Junk: ggg Product Name: AAA CCC DDD #output Group Small AAA 10 BBB 20 Total AAA+BBB = 30 Group Big AAA 10 CCC 30 DDD 40 Total AAA+CCC+DDD = 80
In reply to Re: Extract varibles from 2 text files and make a report
by JediWizard
in thread Extract varibles from 2 text files and make a report
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |