Sekhar Reddy has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

GoodEvening

Can any one help me on this, How this can be done in perl

Thank you very much in advance

I have 4 different Scenarios, I have listed down, and i have also written how my hash should store

STRUCTURE OF MY DATA below(with Header, last two fields are date fields in yyyymmdd format):

A1,B1,ACTDATE,DEACTDATE

7900724666,200906888,20180406,20180411

7900724666,200906888,20180416,20180522

7900724666,200906888,20180601,20180720

For the above content(A1), i want to store in hash like this==>A1,B1 of MIN ACTDATE:B1 of MAX DEACTDATE,MIN ACTDATE,MAX DEACTDATE

For the above example, ==>7900724666,200906888:200906888,20180406,20180720

7900724677,200906871,20180101,20180228

7900724677,200906872,20180301,20180330

7900724677,200906873,20180401,20180420

For the above content(A1), i want to store in hash like this==>7900724677,200906871:200906873,20180101,20180420

7900724688,200906881,20180101,20180228

7900724688,200906881,20180303,20180330

7900724688,200906882,20180404,20180430

7900724688,200906883,20180508,20180620

For the above content(A1), i want to store in hash like this==>7900724688,200906881:200906883,20180101,20180620

7900724699,200906891,20180101,20180228

7900724699,200906891,20180303,20180330

7900724699,200906892,20180404,20180430

7900724699,200906893,20180508,

For the above content(A1), i want to store in hash like this==>7900724699,200906891:NULL,20180101,NULL

Replies are listed 'Best First'.
Re: sorting and merging in perl
by Veltro (Hermit) on Jul 18, 2018 at 19:34 UTC

    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

      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.
Re: sorting and merging in perl
by Anonymous Monk on Jul 18, 2018 at 17:27 UTC

    How are you failing?

      Thank you everyone, i some how able to solve this.

      I did it by finding the count of unique A1's and i stored it in hash as a key, A loop to iterate that many number of times to create a new hash which store the A1 and activedate as a key.

      Sort by active date

      find the consecutive rows by having a count(unique A1 and B1).