Hello guys, I have two files: File A
02,29031990,feb,21,5 02,29031990,feb,18,5 02,29031990,feb,20,5 02,29031990,feb,21,5
File B
Unit4872,29031990,01,0 Unit4873,29031990,03,0 Unit4874,29031990,02,4 Unit4875,29031990,02,3 Unit4876,29031990,02,2 Unit4877,29031990,02,1 Unit4878,29031990,02,0
My keys I have matched are 1st with 2nd column from File A and 2nd with 3rd from file B. My rule is when both keys matched I will print 1st column from File B with 2nd, 1st, 3rd, 4th and 5th from File A and one counter of all rows of the File A. So, with combined these two files I have created this file:
Unit4874,29031990,02,feb,21,5,1 Unit4875,29031990,02,feb,18,5,2 Unit4876,29031990,02,feb,20,5,3 Unit4877,29031990,02,feb,21,5,4
Now my situation is this. If my last number of my counter(in this case is 4) minus the 5th column from File A is 1, I have to get next line from File B adding add 1 more to counter and getting the last result of 4th column from File B. My result would be like that.
Unit4874,29031990,02,feb,21,5,1 Unit4875,29031990,02,feb,18,5,2 Unit4876,29031990,02,feb,20,5,3 Unit4877,29031990,02,feb,21,5,4 Unit4878,29031990,02,feb,21,5,5.
This is my code so far. I was testing but I am caught in this code.
#!/usr/bin/perl use strict; use warnings; use Data::Dumper qw(Dumper); my $filea = $ARGV[0]; my $fileb = $ARGV[1]; open ( FA, '<', $filea) || die ( "File $filea Not Found!" ); open ( FB, '<', $fileb) || die ( "File $fileb Not Found!" ); my %ts; my @LAST_PLC; while ( <FB> ) { chomp; my($Unit, $date, $mnt, $qtde) = split ","; push @{ $ts{$date,$mnt} }, $Unit; push @LAST_PLC, ($Unit); } my $last_one = pop @LAST_PLC; #print $last_one."\n"; my $count=1; my @FIRST_PRICE; my $result; my @FINAL; my $price; while ( <FA> ) { chomp; my($mnt,$date,$att,$idx,$max) = split ","; push @FIRST_PRICE, ($idx); $price = shift @FIRST_PRICE; #if ( exists( $ts{$date,$mnt} ) ){ my $Unit = shift @{$ts{$date,$mnt}}; if ( ( $max - $count ) eq 1 ) { #print join(",", $last_one, $date, $mnt, $att, + $idx, $max, $count)."\n"; #} else { print join(",", $Unit, $date, $mnt, $att, $idx +, $max, $count)."\n"; } #push @FINAL, (join(",", $Unit, $date, $mnt, $att, $id +x, $max, $count, $last_one)."\n"); $count++; #} } #print @FINAL;

In reply to Controlling the count in array by EBK

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.