Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have 2 files:
File A:
AAA|10
BBB|20
CCC|30
DDD|40
more

File B:
> Group Name: Small
Junk: lll
Product Name:
AAA
BBB
Group Name: Big
Junk: ggg
Product Name:
AAA
CCC
DDD
more
I need to print out:
Group Small:
AAA 10
BBB 20
Total AAA+BBB=20
Group Big:
AAA 10
CCC 30
DDD 40
Total AAA+CCC+DDD=80

  • Comment on Extract varibles from 2 text files and make a report

Replies are listed 'Best First'.
Re: Extract varibles from 2 text files and make a report
by JediWizard (Deacon) on Oct 22, 2004 at 14:47 UTC

    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
    May the Force be with you
      If the File B like:
      Group Name: Small
      Junk: lll
      Product Name:
      AAA "This is test1 for me."
      BBB "This may be I like it."
      Group Name: Big
      Junk: ggg
      Product Name:
      AAA "This is t2."
      CCC "This should be ok"
      DDD "This is the last one. we shoud have more."

      How do I split the string of each product? The output is same as before.

        If your goal is to discard everything following "AAA" or "BBB", and so on, you could modify the split statement to look like this:

        @prods = split(/\s[^\n]+\n/, $prod);

        This will remove everything following the first whitespace character in each of the lines following "Product Name:". Is that what you were trying to do?

        May the Force be with you
Re: Extract varibles from 2 text files and make a report
by insaniac (Friar) on Oct 22, 2004 at 14:58 UTC
    hm
    you could do this:
    my(%file_a); open(FILEA,"path_to_file_a"); open(FILEB,"path_to_file_b"); while(my $line = <FILEA>){ chomp($line); my @values = split /|/, $line; $file_a{$values[0]}=$values[1]; } close(FILEA); my ($calc_prod,$total); while(my $line = <FILEB>) { chomp($line); if($line =~ m/^Group Name/) { print "Total: $total\n" if $total > 0; print "Group ", {$line =~ m/^Group Name: (.*)$/}, "\n"; $calc_prod = 0; $total = 0; } next if $line =~ m/^Junk/; if($line =~ m/^Product Name/) { $calc_prod = 1; next; } if ($calc_prod) { print "$line ",$file_a{$line}; $total += $file_a{$line}; } } close(FILEB);
    I wrote this without checking the syntax, but you'll see what we're trying to do ;-)
    --
    to ask a question is a moment of shame
    to remain ignorant is a lifelong shame