0

I'm not even sure how to search if this is an existing questions. Let me just give an example:

Call Instance   Date Created           Resource    Resource Status
------------------------------------------------------------------
6557            2013-07-12 11:34:19    cwood       Accepted
6556            2013-07-12 11:34:18    cwood       Accepted
                2013-07-12 11:29:25    cwood       Ready
6555            2013-07-12 09:24:41    cwood       Accepted

How do I get the top two Accepted entries off the top without getting the last Accepted entry (as it came before a Ready entry)?

All the fields except for Date Created are user-defined classes (Call, User [no, not Resource], and ResourceStatus).

Please let me know if further code would be useful.

Micha
  • 5,117
  • 8
  • 34
  • 47
Charles Wood
  • 864
  • 8
  • 23

2 Answers2

1

You can use takeWhile():

​assert [1,2] == [1,2,3,4,5].takeWhile { it < 3 }​

EDIT(Audacity of @dmahapatro to edit answer)
Small test case for available statuses other than Accepted and Ready:

def list = ['A', 'A', 'B', 'A', 'R', 'A']
assert list.takeWhile{ it == 'A'} == ['A', 'A']
assert list.takeWhile{ it != 'R'} == ['A', 'A', 'B', 'A']
dmahapatro
  • 49,365
  • 7
  • 88
  • 117
doelleri
  • 19,232
  • 5
  • 61
  • 65
  • Whoaaaaa. I knew there would be something Groovier than looping through it! Time to check it out. – Charles Wood Jul 12 '13 at 19:04
  • Boom! Like magic! For future answer seekers, I grabbed about 10 ResourceHistory's off the top of the table and fed them into takeWhile with the test of statusDescription == "Accepted". It only gave me the first 2, exactly as needed. Thanks again @doelleri! – Charles Wood Jul 12 '13 at 19:53
  • 1
    @CharlesWood Will you only have `Accepted` and `Ready` statuses available? Your approach is correct in that case. But if you have more statuses and you want all the `Accepted` statuses before `Ready` then you have to use `statusDescription != 'Ready'`. See the update above. – dmahapatro Jul 12 '13 at 20:22
  • Oh, that's a good point @dmahapatro. Occasionally a user may go Busy while working on a call, and I'll want to get any Accepted statuses that occur before the Busy but after the Ready ("before" being "to the right of"), AND not get the Busy either. So, I suppose I'll need to do `statusDescription != 'Ready' and statusDescription != 'Busy'` or so. Code sure would be a lot cleaner without people having to use it! – Charles Wood Jul 12 '13 at 21:06
  • @dmahapatro -- nope, that still only gets the first 2 `A`s. Any idea how to get 3 `A`s out of the above list? – Charles Wood Jul 12 '13 at 21:15
  • Eh, I'll just have them go back into Busy state if they are Busy when they log out. It's more consistent anyway. – Charles Wood Jul 12 '13 at 21:43
  • @CharlesWood I tried something anyways. :) Thought I would post it as answer (no obligation for votes :) ) for better formatting instead of posting as comment. – dmahapatro Jul 12 '13 at 21:53
1

Logic for the below problem statement:

Oh, that's a good point @dmahapatro. Occasionally a user may go Busy while working on a call, and I'll want to get any Accepted statuses that occur before the Busy but after the Ready ("before" being "to the right of"), AND not get the Busy either. So, I suppose I'll need to do statusDescription != 'Ready' and statusDescription != 'Busy' or so. Code sure would be a lot cleaner without people having to use it!

def list = ['A', 'A', 'A', 'R', 'A', 'A','B', 'A']

def statusBeforeBusy = list.takeWhile{ it != 'B'}
println "Statuses before Busy: $statusBeforeBusy" //[A, A, A, R, A, A]

def statusBeforeReady = list.takeWhile{ it != 'R'}
println "Statuses before Ready: $statusBeforeReady" //[A, A, A]

def statusaAfterReadyBeforeBusy = statusBeforeBusy.dropWhile{ it != 'R'}.tail()
println "Statuses After Ready before Busy: $statusaAfterReadyBeforeBusy" //[A, A]
dmahapatro
  • 49,365
  • 7
  • 88
  • 117
  • Well, as I said, I'm just to get everything to the left of the 'R'. Thanks for the code though, this is still Groovier than a for loop or something. – Charles Wood Jul 13 '13 at 17:57