in reply to Loop through two files in comparison

"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

Replies are listed 'Best First'.
Re^2: Loop through two files in comparison
by inperlquest (Initiate) on Aug 13, 2012 at 05:49 UTC

    Thanks for all that. This being my first post on the forum, I'll bite the dust.

    Unfortunately the perl version that is available here is 5.005_02. Also, I'm not familiar with the hash, could you please throw light on what was done in your code? Is it possible to replicate this using arrays? Thanks a ton!

      If at all possible you should update to a more recent version of Perl. The language has moved on a long way since that very old version.

      Perl's hash data type is fundamental to using Perl well. If you expect to write Perl rather than C (or some other simple language) using Perl then it really will repay the effort many times over to learn about the power of Perl's data types. See perldata and Not Exactly a Hash Tutorial (along with anything else in the Tutorials section that looks interesting). When you have worked your way through that lot come back and take another look at the sample code and ask a few specific questions.

      And no, you can't use arrays in place of hashes.

      True laziness is hard work

      Just to put GrandFather's comment about updating to a newer version of Perl into perspective...

      Perl 5.005_02 was released 14 years ago (1998-Aug-08).

        Thanks to you both! Where can I get the latest perl to download and install? I've looked at CPAN & assume this is the right place - http://www.cpan.org/src/5.0/perl-5.16.1.tar.gz ?! Appreciate all advice.