Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

little algorithm help

Options
  • 04-07-2008 8:48am
    #1
    Registered Users Posts: 26,579 ✭✭✭✭


    i have a text file that's comma delimited that i need to process.

    the structure of the file is like so...
    feature | label | date |number of people | preptime | majorfaults | minorfaults
    feature1,label1,,13/06/2008,5,5,2,4
    feature1,label3,,...................,5,4,6
    
    and so on
    

    what i want to be able to do is to sum up the majorfaults and minor faults for each feature.

    so in this case i want the sumMajors to be 6 and the sumMinors to be 10.

    there are hundreads of features in this text file.

    i'm using perl for this by the way here's what i have so far.

    [php]
    //open the file
    open ("FILE", "file.txt");
    my @file = <FILE>;

    my $prevFeature = "";
    my $sumMajors;
    my $sumMinors;

    foreach my $line(@file)
    {
    ($feature,$label,$date,$people,$prepTime, $majors, $minors) = split(",",$line);

    //check to see if the feature is equal to previous feature.
    if($prevFeature eq $feature)
    {
    //sum them up
    $sumMajors += $majors ;
    $sumMinors += $minors ;
    }
    else
    {
    //reset the sum values
    $sumMajors = 0 ;
    $sumMinors = 0 ;
    }

    //i print out the results here.
    print "sumMajors: " . $sumMajors . "\n" ;
    print "sumMinors: " . $sumMinors . "\n" ;

    //make the prevFeature equal to the current feature for the next check.
    $prevFeature = $feature;

    [/php]

    but what i get is the total being +1 of what it should be.

    i think the algorithm is correct but then again it can't be :confused:


Comments

  • Registered Users Posts: 106 ✭✭sham08


    Cremo wrote: »
    ($feature,$label,$date,$people,$prepTime, $majors, $minors) = split(",",$line);
    are you missing $product? Sorry haven't done perl so I'm not sure of the convention


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    sham08 wrote: »
    are you missing $product? Sorry haven't done perl so I'm not sure of the convention

    whoops sorry product isn't contained in the file, i had to type that bit out as for some reason my copy and paste isn't working atm :confused:

    i've edited the OP.


  • Registered Users Posts: 2,150 ✭✭✭dazberry


    Not sure, but what I did notice was that I think you'll need to change the "summing" code as follows:
          if($prevFeature eq $feature)
          {
                 //sum them up
                 $sumMajors += $majors ; 
                 $sumMinors += $minors ;
          }
          else
          {
                 //reset to current line values
                 $sumMajors = $majors ; 
                 $sumMinors = $minors ;
          }
    

    as it looks to me that you'll drop the major and minor fault values of the current line if the feature changes. I don't know if you'll need to specifically set the sum*** variable to 0 prior to the loop or if they're already initiated to 0 in Perl by default.

    D.


  • Registered Users Posts: 26,579 ✭✭✭✭Creamy Goodness


    Perfect Daz,

    that worked. I knew it had to of been something simple. I was writing that last night still in work at 8:30 last night :(


Advertisement