"What am I doing wrong?"
  1. Completely misunderstanding the task at hand
  2. Not using strictures. Always use strictures (use strict; use warnings; - see The strictures, according to Seuss)
  3. Not providing a small sample script that demonstrates your problem
  4. Showing us code that can't generate the output shown
  5. Using bare word file handles instead of lexical file handles
  6. Using & (a bitwise operator) instead of && (a logical operator)
  7. Using a C for loop rather than a Perl style loop (for my $i (0 .. $inv1[0] - 1) {...
  8. Useless use of a private variable in a void context ($line2 inside the for loop)
  9. 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

In reply to Re: Loop through two files in comparison by GrandFather
in thread Loop through two files in comparison by inperlquest

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.