in reply to Loop through two files in comparison
"What am I doing wrong?"
Well, you did ask! Probably there is a bunch of other stuff too, but that's enough to be going on with. An first approximation to a solution to the problem you imply could look like:
#!/usr/bin/perl use strict; use warnings; my $f1Data = <<F1; 1,1234 3,2345 F1 my $f2Data = <<F2; 1234,ABC,10 2345,PQR,34 2345,XYZ,37 2345,LMN,14 F2 my %lu; open my $f1In, '<', \$f1Data; while (defined (my $line = <$f1In>)) { chomp $line; my ($tagId, $value) = split ',', $line; next if ! defined $value; $lu{$value}{tagid} = $tagId; } open my $f2In, '<', \$f2Data; while (defined (my $line = <$f2In>)) { chomp $line; my ($key, $label, $value) = split ',', $line; next if ! defined $value || !exists $lu{$key}; $lu{$key}{children} .= <<CHILD; <tag$lu{$key}{tagid}_1>$label</tag$lu{$key}{tagid}_1> <tag$lu{$key}{tagid}_2>$value</tag$lu{$key}{tagid}_2> CHILD } for my $key (sort keys %lu) { print <<PARENT; <tag$lu{$key}{tagid}> $lu{$key}{children}</tag$lu{$key}{tagid}> PARENT }
prints:
<tag1> <tag1_1>ABC</tag1_1> <tag1_2>10</tag1_2> </tag1> <tag3> <tag3_1>PQR</tag3_1> <tag3_2>34</tag3_2> <tag3_1>XYZ</tag3_1> <tag3_2>37</tag3_2> <tag3_1>LMN</tag3_1> <tag3_2>14</tag3_2> </tag3>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Loop through two files in comparison
by inperlquest (Initiate) on Aug 13, 2012 at 05:49 UTC | |
by GrandFather (Saint) on Aug 13, 2012 at 06:29 UTC | |
by Mr. Muskrat (Canon) on Aug 13, 2012 at 20:44 UTC | |
by inperlquest (Initiate) on Aug 14, 2012 at 03:10 UTC | |
by Mr. Muskrat (Canon) on Aug 14, 2012 at 04:42 UTC |