Well ... I could be wrong (I'm no perl guru) but .. why not first cut up the string into H's and T's, place them into an array. Now create a flag
as an example and then create a simple for loop, where you go though the array and create the appropriate if else statements to get the info you need.