to parse a list of records....

Discussion in 'Perl Misc' started by Jim Carter, Aug 20, 2003.

  1. Jim Carter

    Jim Carter Guest

    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
    Jim Carter, Aug 20, 2003
    #1
    1. Advertising

  2. Jim Carter

    Steven Kuo Guest

    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
    Steven Kuo, Aug 20, 2003
    #2
    1. Advertising

  3. 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
    John W. Krahn, Aug 20, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luke Airig
    Replies:
    0
    Views:
    788
    Luke Airig
    Dec 31, 2003
  2. Replies:
    19
    Views:
    1,121
    Daniel Vallstrom
    Mar 15, 2005
  3. Dan

    Delete records or update records

    Dan, May 10, 2004, in forum: ASP General
    Replies:
    1
    Views:
    461
    Ray at
    May 10, 2004
  4. Replies:
    3
    Views:
    656
    Anthony Jones
    Nov 2, 2006
  5. 7stud --

    optparse: parse v. parse! ??

    7stud --, Feb 20, 2008, in forum: Ruby
    Replies:
    3
    Views:
    185
    7stud --
    Feb 20, 2008
Loading...

Share This Page