to parse a list of records....

J

Jim Carter

Hi experts,

I have the below issue:

I have an input table like this:

----- ---------- --------
Year Quarter Value
----- ---------- --------
2003 Q1 15.5
2003 Q1 7.5
2003 Q2 5.5
2003 Q4 10
2003 Q4 12.2
2003 Q2 11
2004 Q1 15.4
2004 Q2 17
2004 Q4 18
2004 Q3 12.5
2004 Q4 9.9
----------------------------------------------------

Now I want to populate the following combinations of year-quarter into
some variables ($a, $b, $c, $d etc) and summation calculations:

The combination should be in sequence (from youngest to oldest):

$a = concatenation of youngest combination (in this case, it is
2003-Q1).
$total1 = summation of all "Values" in the 2003-Q1 combination.
$b = concatenation of second youngest combination (in this case, it is
2003-Q2).
$total2 = summation of all "Values" in the 2003-Q2 combination.
$c = concatenation of third youngest combination (in this case, it is
2003-Q4).
$total3 = summation of all "Values" in the 2003-Q4 combination.
$d = concatenation of fourth youngest combination (in this case, it is
2004-Q1).
$total4 = summation of all "Values" in the 2004-Q1 combination.
$e = concatenation of fifth youngest combination (in this case, it is
2004-Q2).
$total4 = summation of all "Values" in the 2004-Q2 combination.
and so on....

Result should be :

$a = 2003-Q1
$value1 = 23
$b = 2003-Q2
$value2 = 16.5
$c = 2003-Q4
$value3 = 22.2
$d = 2004-Q1
$value4 = 15.4
$e = 2004-Q2
$value5 = 17
$f = 2004-Q3
$value6 = 12.5
$g = 2004-Q4
$value7 = 27.9

Can any one suggest a perl script to do this job? I am struggling with
the loops and arrays.

Thanks,
Jim
 
S

Steven Kuo

Hi experts,

I have the below issue:

I have an input table like this:

----- ---------- --------
Year Quarter Value
----- ---------- --------
2003 Q1 15.5
2003 Q1 7.5
2003 Q2 5.5
2003 Q4 10
2003 Q4 12.2
2003 Q2 11
2004 Q1 15.4
2004 Q2 17
2004 Q4 18
2004 Q3 12.5
2004 Q4 9.9
----------------------------------------------------

Now I want to populate the following combinations of year-quarter into
some variables ($a, $b, $c, $d etc) and summation calculations:

The combination should be in sequence (from youngest to oldest):

(snipped)


The names you've chosen for variables (e.g., $a, $b, etc.) aren't
very descriptive.

Result should be :

$a = 2003-Q1
$value1 = 23
$b = 2003-Q2
$value2 = 16.5
$c = 2003-Q4
$value3 = 22.2
$d = 2004-Q1
$value4 = 15.4
$e = 2004-Q2
$value5 = 17
$f = 2004-Q3
$value6 = 12.5
$g = 2004-Q4
$value7 = 27.9

Can any one suggest a perl script to do this job? I am struggling with
the loops and arrays.

Thanks,
Jim


In this case, forego the array and use a hash:

#!/usr/local/bin/perl

use strict;
use warnings;

my %total;

while (<DATA>) {
if (/^(\d{4})\s+(Q[1-4])\s+([\d.]+)/) {
$total{"$1-$2"} += $3;
}
}

for my $fy_quarter (sort keys %total) {
printf "For $fy_quarter the total value was %0.2f\n", $total{$fy_quarter};
}

__DATA__
----- ---------- --------
Year Quarter Value
----- ---------- --------
2003 Q1 15.5
2003 Q1 7.5
2003 Q2 5.5
2003 Q4 10
2003 Q4 12.2
2003 Q2 11
2004 Q1 15.4
2004 Q2 17
2004 Q4 18
2004 Q3 12.5
2004 Q4 9.9
 
J

John W. Krahn

Jim said:
I have the below issue:
I have an input table like this:
----- ---------- --------
Year Quarter Value
----- ---------- --------
2003 Q1 15.5
2003 Q1 7.5
2003 Q2 5.5
2003 Q4 10
2003 Q4 12.2
2003 Q2 11
2004 Q1 15.4
2004 Q2 17
2004 Q4 18
2004 Q3 12.5
2004 Q4 9.9
----------------------------------------------------

Now I want to populate the following combinations of year-quarter into
some variables ($a, $b, $c, $d etc) and summation calculations:

The combination should be in sequence (from youngest to oldest):

$a = concatenation of youngest combination (in this case, it is 2003-Q1).
$total1 = summation of all "Values" in the 2003-Q1 combination.
$b = concatenation of second youngest combination (in this case, it is 2003-Q2).
$total2 = summation of all "Values" in the 2003-Q2 combination.
$c = concatenation of third youngest combination (in this case, it is 2003-Q4).
$total3 = summation of all "Values" in the 2003-Q4 combination.
$d = concatenation of fourth youngest combination (in this case, it is 2004-Q1).
$total4 = summation of all "Values" in the 2004-Q1 combination.
$e = concatenation of fifth youngest combination (in this case, it is 2004-Q2).
$total4 = summation of all "Values" in the 2004-Q2 combination.
and so on....

Result should be :

$a = 2003-Q1
$value1 = 23
$b = 2003-Q2
$value2 = 16.5
$c = 2003-Q4
$value3 = 22.2
$d = 2004-Q1
$value4 = 15.4
$e = 2004-Q2
$value5 = 17
$f = 2004-Q3
$value6 = 12.5
$g = 2004-Q4
$value7 = 27.9

Can any one suggest a perl script to do this job? I am struggling with
the loops and arrays.


You should probably use a hash, something like this:

$; = '-';
my %data;
while ( <DATA> ) {
next unless /\d/;
my @fields = split;
$data{ $fields[0], $fields[1] } += $fields[2];
}

for my $key ( sort keys %data ) {
print "$key\t$data{$key}\n";
}

__DATA__
----- ---------- --------
Year Quarter Value
----- ---------- --------
2003 Q1 15.5
2003 Q1 7.5
2003 Q2 5.5
2003 Q4 10
2003 Q4 12.2
2003 Q2 11
2004 Q1 15.4
2004 Q2 17
2004 Q4 18
2004 Q3 12.5
2004 Q4 9.9
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top