"What am I doing wrong?"
- Completely misunderstanding the task at hand
- Not using strictures. Always use strictures (use strict; use warnings; - see The strictures, according to Seuss)
- Not providing a small sample script that demonstrates your problem
- Showing us code that can't generate the output shown
- Using bare word file handles instead of lexical file handles
- Using & (a bitwise operator) instead of && (a logical operator)
- Using a C for loop rather than a Perl style loop (for my $i (0 .. $inv1[0] - 1) {...
- Useless use of a private variable in a void context ($line2 inside the for loop)
- Reading the files in parallel where you really want to build a lookup table using the first then generate output while reading the second
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>
True laziness is hard work
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.