Hi Sekhar Reddy,

I think something like this can help you out:

use strict ; use warnings ; my %results = () ; while ( <DATA> ) { chomp ; my @row = split /,/, $_ ; if ( exists $results{ $row[0] } ) { # Update existing hash entries if ( ( $row[2] ) < $results{ $row[0] }->{ 'MIN ACTDATE' } ) { $results{ $row[0] }->{ 'B1 MIN ACTDATE' } = $row[1] ; $results{ $row[0] }->{ 'MIN ACTDATE' } = $row[2] ; } if ( !( defined $row[3] && defined $results{ $row[0] }->{ 'MAX DEACTDATE' } ) ) { $results{ $row[0] }->{ 'B1 MAX ACTDATE' } = undef ; $results{ $row[0] }->{ 'MAX DEACTDATE' } = undef ; } elsif ( ( $row[3] ) > ( $results{ $row[0] }->{ 'MAX DEACTDATE' } ) ) { $results{ $row[0] }->{ 'B1 MAX ACTDATE' } = $row[1] ; $results{ $row[0] }->{ 'MAX DEACTDATE' } = $row[3] ; } } else { # Create new entry in hash $results{ $row[0] } = { 'A1' => $row[0], 'B1 MIN ACTDATE' => $row[1], 'B1 MAX ACTDATE' => $row[1], 'MIN ACTDATE' => $row[2], 'MAX DEACTDATE' => $row[3], } } } foreach ( sort keys %results ) { my $a1 = $results{ $_ }->{ 'A1' } ; my $b1ma = $results{ $_ }->{ 'B1 MIN ACTDATE' } ; my $b1md = $results{ $_ }->{ 'B1 MAX ACTDATE' } // 'NULL' ; my $mad = $results{ $_ }->{ 'MIN ACTDATE' } ; my $mdad = $results{ $_ }->{ 'MAX DEACTDATE' } // 'NULL' ; print "$a1,$b1ma:$b1md,$mad,$mdad\n" ; } __DATA__ 7900724666,200906888,20180416,20180522 7900724666,200906888,20180601,20180720 7900724666,200906888,20180406,20180411 7900724677,200906872,20180301,20180330 7900724677,200906871,20180101,20180228 7900724677,200906873,20180401,20180420 7900724688,200906881,20180101,20180228 7900724688,200906881,20180303,20180330 7900724688,200906882,20180404,20180430 7900724688,200906883,20180508,20180620 7900724699,200906891,20180101,20180228 7900724699,200906891,20180303,20180330 7900724699,200906892,20180404,20180430 7900724699,200906893,20180508,

Assuming the first columns is a unique key, I am using a hash to store the data for that unique key.

The output is:

7900724666,200906888:200906888,20180406,20180720 7900724677,200906871:200906873,20180101,20180420 7900724688,200906881:200906883,20180101,20180620 7900724699,200906891:NULL,20180101,NULL

With best regards,
Veltro


In reply to Re: sorting and merging in perl by Veltro
in thread sorting and merging in perl by Sekhar Reddy

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.