Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.
Hi all, please see this major site announcement: https://www.boards.ie/discussion/2058427594/boards-ie-2026

little algorithm help

  • 04-07-2008 07:48AM
    #1
    Registered Users, Registered Users 2 Posts: 26,449 ✭✭✭✭


    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, Registered Users 2 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, Registered Users 2 Posts: 26,449 ✭✭✭✭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, Registered Users 2 Posts: 2,157 ✭✭✭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, Registered Users 2 Posts: 26,449 ✭✭✭✭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