Hey Monks
this question is not so much a question for which I require a Monk to supply code but rather I require a Monk to use his Perl Wisdom to explain some code to me. The code below does the following. It is supplied with data of the following type.
-,--,--,--,1.280000e+2,9.930000e+0 --,--,--,--,1.920000e+2,9.950000e+0 --,--,--,--,2.560000e+2,1.013000e+1 --,--,--,--,2.000000e+0,4.370000e+0 --,--,--,--,4.000000e+0,5.300000e+0 --,--,--,--,8.000000e+0,6.590000e+0 --,--,--,--,1.600000e+1,7.830000e+0 --,--,--,--,2.400000e+1,8.710000e+0 --,--,--,--,3.200000e+1,9.160000e+0 --,--,--,--,6.400000e+1,9.510000e+0 --,2.000000e+0,6.500000e+0,--,--,-- --,2.000000e+0,6.450000e+0,--,--,-- --,4.000000e+0,6.650000e+0,--,--,-- --,4.000000e+0,6.570000e+0,--,--,-- --,8.000000e+0,6.550000e+0,--,--,-- --,8.000000e+0,6.600000e+0,--,--,-- --,1.600000e+1,6.570000e+0,--,--,-- --,1.600000e+1,6.570000e+0,--,--,-- --,2.400000e+1,6.650000e+0,--,--,-- --,2.400000e+1,6.680000e+0,--,--,-- --,2.400000e+1,6.640000e+0,--,--,-- --,3.200000e+1,6.720000e+0,--,--,--
It then sorts the data into the following format
2.000000e+0,4.370000e+0,2.000000e+0,6.500000e+0 4.000000e+0,5.300000e+0,4.000000e+0,6.650000e+0 8.000000e+0,6.590000e+0,8.000000e+0,6.550000e+0 1.600000e+1,7.830000e+0,1.600000e+1,6.570000e+0 2.400000e+1,8.710000e+0,2.400000e+1,6.650000e+0 3.200000e+1,9.160000e+0,3.200000e+1,6.720000e+0 1.280000e+2,9.930000e+0,-,- 1.920000e+2,9.950000e+0,--,-- 2.560000e+2,1.013000e+1,--,-- 6.400000e+1,9.510000e+0,--,-- --,--,2.000000e+0,6.450000e+0 --,--,4.000000e+0,6.570000e+0 --,--,2.400000e+1,6.640000e+0 --,--,8.000000e+0,6.600000e+0 --,--,1.600000e+1,6.570000e+0 --,--,2.400000e+1,6.680000e+0

What it does is match up anything from the first half whose fifth value is equal to anything in the second halfs second value. Confused? Imagine that the rows begining with --, --, --, -- are the first half and the rest the second. So now we have two seperate sets. Now ignore all "--". This leaves you with two sets of two columns. What it does is match the first in each of these sets. i.e. above the fourth row in the first half matched the first row in the second half.
I need to expand on this code to be able to handle more data with variable length of columns, match different coulmns etc. However I don't know how this code works. Hence this wisdom I seek is........How does this code work?

Thanks in advance Monks

#!/usr/bin/perl -w my @L = (); my @R = (); my $file = "< SqlResults_full"; open(DATA, $file) or die "Can\'t open " . $file . " for output : $!"; while(<DATA>) { # BUILD @LIST chomp; next unless $_; my @Line = split ',', $_; if($Line[4] ne '--') { # 5th value real? push @R, \@Line; } elsif($Line[1] ne '--') { #2nd value real? push @L, \@Line; } } $\="\n"; print "R ".scalar(@R); print "L ".scalar(@L); print "TOTAL LINES ".( @R + @L ); use Data::Dumper; COMPARE(\@L,\@R); sub COMPARE { my( $L, $R ) = @_; my @Ret = (); my %L = map { $_ => $_; } 0..$#$L; my %R = map { $_ => $_; } 0..$#$R; for my $I(0..$#$R ) { for my $J(0..$#$L ) { if($R->[$I]->[4] eq $L->[$J]->[1]) { next unless exists $L{$J}; delete $L{$J}; delete $R{$I}; print join ',', @{ $R->[$I] }[4,5], @{ $L->[$J] }[1,2] +; last; } } } print join ',', @{ $R->[$_] }[4,5,0,0] for keys %R; print join ',', @{ $L->[$_] }[0,0,1,2] for keys %L; }

j o h n i r l .

Sum day soon I'Il lern how 2 spelI (nad tYpe)


In reply to Enlightenment by johnirl

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.