Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2007
    Posts
    5

    Unanswered: Need Help with looping through file and grouping rows

    Hi All

    I have a problem in grouping rows and creating a vector.

    A,180,12.345
    A,365,67.890
    B,10950,78.987
    B,1095,67.123


    In my sample file, the first column is the ID, the second column the date in days and the final column is a value. For rows with the same ID, I need to group them and create a vector with the dates, their corresponding value and the sequence they should get loaded into the system. So the resultant output will look something like this.

    BAS,A
    ro,180,12.345,1
    ro,365,67.890,2
    BAS,B
    ro,1095,67.123,1
    ro,10950,78.987,2

    I've tried to hack together something but my feeling is that its way off as I'm not that familiar with hashes which I've been advised is the way to go here.

    if (exists $sh_ids->{$id}) { sh_ids->{$id}++}
    else {sh_ids->{$id} = 0}

    foreach $key(sort keys %{$sh_ids})
    {
    print OUTFILE "BAS,$id\n";
    for ($i=0; $i < $sh_ids->{$key}; $i++ $seq++)
    {
    print OUTFILE "ro,$date,$value,$seq\n";
    }
    }

    As always, any help would be appreciated.

    TIA..

  2. #2
    Join Date
    Feb 2007
    Posts
    5
    Okay, problem solved. This is the end result. Doesn't look pretty but it works.

    while (<INFILE>)

    {
    chomp;
    $lines=$_;

    @part=split /,/, $lines;
    $id = $part[4];
    $NAME = $part[5];
    $SPOTPRICE = $part[7];
    @SPOTS=split /\ /, $SPOTPRICE;
    $SPOTPRICEUNIT = $SPOTS[1];
    $SPOTPRICEVAL = $SPOTS[0];
    $CURRENCYUNIT = $part[6];
    $THEOMODELXREF = $part[11];
    $FACTORXREF = $part[8];
    $FACTTERMVAL = $part[9];
    $FIRSTSENVAL = $part[10];


    if (exists $sh_ids->{$id}) {$sh_ids->{$id}->{'count'}++ }
    else {$sh_ids->{$id}->{'count'}=0}
    $sh_ids->{$id}->{'spotunit'}=$SPOTPRICEUNIT;
    $sh_ids->{$id}->{'spotval'}=$SPOTPRICEVAL;
    $sh_ids->{$id}->{'theoxref'}=$THEOMODELXREF;
    $sh_ids->{$id}->{'currency'}=$CURRENCYUNIT;
    $sh_ids->{$id}->{'term'}->{$FACTTERMVAL}->{'factorxref'}=$FACTORXREF;
    $sh_ids->{$id}->{'term'}->{$FACTTERMVAL}->{'firstsenval'}=$FIRSTSENVAL;
    }



    foreach $key(sort keys %{$sh_ids})
    {
    print OUTFILE "BAS,Sensitivity InstrumentSPEC,Sensitivity InstrumentSPEC,Sensitivity Instrument,$key,$key,$sh_ids
    ->{$key}->{'spotval'},$sh_ids->{$key}->{'spotunit'},$sh_ids->{$key}->{'spotunit'},$sh_ids->{$key}->{'currency'},$sh_ids->{$ke
    y}->{'theoxref'}\n";
    foreach $term(sort keys %{$sh_ids->{$key}->{'term'}})
    {
    print OUTFILE "rm_ro,Sensitivity InstrumentSPEC: Factors,Factors,Sensitivity InstrumentSPEC,$sh_ids->{$key}->{'t
    erm'}->{$term}->{'factorxref'}\n";
    print OUTFILE "rm_ro,Sensitivity InstrumentSPEC: Factor Terms,Factor Terms,Sensitivity InstrumentSPEC,$term\n";
    print OUTFILE "rm_ro,Sensitivity InstrumentSPEC: 1st Order Sensitivities,1st Order Sensitivities,Sensitivity Ins
    trumentSPEC,$sh_ids->{$key}->{'term'}->{$term}->{'firstsenval'}\n";
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •