Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: sorting and merging in perl

by Veltro (Hermit)
on Jul 18, 2018 at 19:34 UTC ( [id://1218780]=note: print w/replies, xml ) Need Help??


in reply to sorting and merging in perl

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

Replies are listed 'Best First'.
Re^2: sorting and merging in perl
by Sekhar Reddy (Acolyte) on Jul 23, 2018 at 07:00 UTC
    Thank you somuch Veltro, Greatly appreciated your help. Once again thank you
A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (8)
As of 2024-04-25 11:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found