Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Loop through two files in comparison

by inperlquest (Initiate)
on Aug 13, 2012 at 01:58 UTC ( [id://987015]=perlquestion: print w/replies, xml ) Need Help??

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

Monks, I'm struggling to find a solution to this seemingly simple problem. Please guide me towards salvation. There are two csv files of which I'm to convert File2 into nested XML. Handcoding the logic, as the library doesn't have the required modules.

File 1 -------
1,1234
3,2345


File 2 ---------
1234,ABC,10
2345,PQR,34
2345,XYZ,37
2345,LMN,14

I am to process File2 using File1 in iterative comparison, but I get the second line from File2 repeated 3 times in the XML result, unlike what I expect

Output as I want it:
<tag1>1234</tag1>
  <tag1_1>ABC</tag1_1>
   <tag1_2>10</tag1_2>
</tag1>
<tag1>2345</tag1>
  <tag1_1>PQR</tag1_1>
  <tag1_2>34</tag1_2>
  <tag1_1>XYZ</tag1_1>
  <tag1_2>37</tag1_2>
  <tag1_1>LMN</tag1_1>
  <tag1_2>14</tag1_2>
</tag1>

I see only the line with PQR from File2 repeating all 3 times in the output now

This is my code:
while (($line1 = <FILE1>) & ($line2 = <FILE2>)) { @inv1 = split (',',$line1); chomp (@inv1); @inv2 = split (',',$line2); chomp (@inv2); if ($inv1[1] == $inv2[0]) { for ($i = 1; $i <= $inv1[0]; $i++) { ## xml printing from file2 $line2; } } } ##while

What am I doing wrong?

Replies are listed 'Best First'.
Re: Loop through two files in comparison
by GrandFather (Saint) on Aug 13, 2012 at 04:02 UTC
    "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

      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).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://987015]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (7)
As of 2024-03-29 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found