I am new to Perl, I wanted to combine two lines where "ANN" and "QTR" in second column have same date in fourth column (date), otherwise just print line with data which has "ANN" in it. It also skip line where date column is empty:

Input data file:

I am new to Perl, I wanted to combine two lines where "ANN" and "QTR" in second column have same date in fourth column (date), otherwise just print line with data which has "ANN" in it an then sort data based on first field. It also skip line where date column is empty:

Input data file
==================
SEC ANN -11 19831117
SEC ANN -11 19831215
SEC QTR -11 0
SEC ANN 1   0
SEC QTR -1.17   0
SEC QTR 1.11    0
SEC QTR 1.66    19861218
SEC ANN -1.36   19860116
SEC ANN -1.52   19861120
SEC ANN -1.52   19861218
SEC QTR 1   0
SEC QTR -1.16   0
SEC QTR 1.12    19860116


output would like like this:
==========================
SEC 19831117    -0.07   0
SEC 19831215    -0.07   0
SEC 19860116    -1.36   1.12
SEC 19860220    -1.36   0
SEC 19861120    -0.52   0
SEC 19861218    -1.52   1.66

SEC ANN -11 19831215
SEC QTR -11 0
SEC ANN 1 0
SEC QTR -1.17 0
SEC QTR 1.11 0
SEC QTR 1.66 19861218
SEC ANN -1.36 19860116
SEC ANN -1.52 19861120
SEC ANN -1.52 19861218
SEC QTR 1 0
SEC QTR -1.16 0
SEC QTR 1.12 19860116

output would like like this:

SEC 19831117 -0.07 0
SEC 19831215 -0.07 0
SEC 19860116 -1.36 1.12
SEC 19860220 -1.36 0
SEC 19861120 -0.52 0
SEC 19861218 -1.52 1.66

Hi newperluser,

I am new to Perl

You are welcome to using Perl, a great programming language and tool.

It would have been wonderful, if you have shown what you have been able to do yourself, then you can get help and correction.

However, I observed that your data is either incomplete or not correct. Becasuse there are other info in your desired output that are not in the raw input. For example, there is nowhere in your input where one can find 0.07 or the fourth column number 19860220. Except such numbers are manufactured or are omitted.

The following codes gives an idea of what you wanted:

use warnings;
use strict;
use Data::Dumper;

my %data;

while (<DATA>) {
    my @file = split;
    push @{ $data{ $file[0] }{ $file[3] }{ $file[1] } } => $file[2];
}
{
local $Data::Dumper::Sortkeys = 1;
print Dumper \%data;
}

__DATA__
SEC ANN -11 19831117
SEC ANN -11 19831215
SEC QTR -11 0
SEC ANN 1   0
SEC QTR -1.17   0
SEC QTR 1.11    0
SEC QTR 1.66    19861218
SEC ANN -1.36   19860116
SEC ANN -1.52   19861120
SEC ANN -1.52   19861218
SEC QTR 1   0
SEC QTR -1.16   0
SEC QTR 1.12    19860116

Which gives an output of:

$VAR1 = {
      'SEC' => {
                 '0' => {
                          'ANN' => [
                                     '1'
                                   ],
                          'QTR' => [
                                     '-11',
                                     '-1.17',
                                     '1.11',
                                     '1',
                                     '-1.16'
                                   ]
                        },
                 '19831117' => {
                                 'ANN' => [
                                            '-11'
                                          ]
                               },
                 '19831215' => {
                                 'ANN' => [
                                            '-11'
                                          ]
                               },
                 '19860116' => {
                                 'ANN' => [
                                            '-1.36'
                                          ],
                                 'QTR' => [
                                            '1.12'
                                          ]
                               },
                 '19861120' => {
                                 'ANN' => [
                                            '-1.52'
                                          ]
                               },
                 '19861218' => {
                                 'ANN' => [
                                            '-1.52'
                                          ],
                                 'QTR' => [
                                            '1.66'
                                          ]
                               }
               }
    };
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.