my @currenttime=(); ## or initialize to the start time my @totaltime=(); ## will hold the sums for each ID foreach (@databaseline) { ## realistically, this will be something like ## while (@values = $sth->fetchrow) when you convert to ## using SQL ( Look at the DBI module and the corresponding ## DBD module for your database ) my ($dbtime,$code,$id)= &parseline($_); ## This routine would split the line and convert ## the date, passing back the useful values. ## When you get it out of SQL, you should have an array ## of values anyway, so it'll only be necessary to ## convert the date. $totaltime[$id]+=($dbtime-$currenttime[$id]) if $code eq 'open'; $currenttime[$id]=$dbtime; ## we move the baseline to the time of the current record } ## now the @totaltime array has the values you want.