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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |