Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > to parse a list of records....

Reply
Thread Tools

to parse a list of records....

 
 
Jim Carter
Guest
Posts: n/a
 
      08-20-2003
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
 
Reply With Quote
 
 
 
 
Steven Kuo
Guest
Posts: n/a
 
      08-20-2003
On 20 Aug 2003, Jim Carter wrote:

> 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
----------------------------------------------------


--
Hope this helps,
Steven

 
Reply With Quote
 
 
 
 
John W. Krahn
Guest
Posts: n/a
 
      08-20-2003
Jim Carter wrote:
>
> 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
----------------------------------------------------



John
--
use Perl;
program
fulfillment
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
optparse: parse v. parse! ?? 7stud -- Ruby 3 02-20-2008 05:20 AM
STL derived class : parse list from inside Vincent RICHOMME C++ 1 11-09-2006 02:10 AM
How to parse a string like C program parse the command line string? linzhenhua1205@163.com C Programming 19 03-15-2005 07:41 PM
using python to parse md5sum list Ben Rf Python 4 03-07-2005 08:14 AM
How to parse a list using pyparsing Nitin Madnani Python 0 07-31-2004 05:27 AM



Advertisments