This question already has answers here:
Can you break from a Groovy "each" closure?
(8 answers)
Closed 10 years ago.
How will I break the loop if a particular condition is satisfied?
My input file has around n number of lines. If the required information is got at some mth line I need to break the loop instead of reading the remaining lines.
new File("test.txt").readLines().reverseEach{line ->
println line
if(line.contains("Data"))
break;
}
You could use find to do this. Find ends the loop the first time the closure returns true and returns the last found element.
This
(1..20).find{
println it
it == 5
}
Would be the following in the groovy console.
1
2
3
4
5
Result: 5
You can't just break from closure. You can either throw an exception and exit from it, or do not use closures at all. Read all lines in array/list and iterate through it.
Related
This question already has answers here:
Why is this printing 'None' in the output? [duplicate]
(2 answers)
Why does the print function return None?
(1 answer)
Closed last year.
print(print("Hello World!"))
and the output is :
Hello World!
None
The print function has no return value, i.e. it returns None.
(What did you expect to get with the second print?)
This question already has answers here:
How to get last 2 digit from a String
(7 answers)
Closed 1 year ago.
input :
DEPT MGR_AEG
output :
AEG
above is the input and expected output from piece of Groovy code. I mean I need to fetch the last 3 characters of input string. how to achieve this in Groovy ?
thanks in advance for your help.
reg, Avinash
The simplest way is a substring expression:'
assert 'DEPT MGR_AEG'[-3..-1] == 'AEG'
You can also use take() but it's somewhat unintuative:
assert 'DEPT MGR_AEG'.reverse().take(3).reverse() == 'AEG'
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
array = [1,2,3,3,3,3,9,4,5]
for number in range(1,length(array)+1):
pass
print(number) #This here print "9"
pass is actually used for when the person wants to make a while loop or for loop etc to be empty or to do nothing in the loop..What you did is after the pass statement you put a print statement so the python Interpreter simply passes through the for loop doing nothing and at the end of the for loop the value of number becomes 9 so it is printed by the followed statement print(number) .
The pass statement in Python does not work like a comment. The compiler will simply "pass" the pass statement and move on to the next which, in this case, is the print.
print function will print whatever instruction will be given to it.
Here range is playing role in printing the number.
array size is 9 in your example and as per range function definition "it generate a sequence of integers from start (including) to end (excluding) by step."
So as we have a input for number in range(1,len(array)+1):
so it will start from 1 to 10 and your program will be like
array = [1,2,3,3,3,3,9,4,5]
for number in range(1,len(array)+1):
pass
print(number)
So number will be having 9 as a last value to hold and print that's why it is printing 9.
This question already has answers here:
Why are empty strings returned in split() results?
(9 answers)
Closed 2 years ago.
I have this String and I want to separate it using the specified separator. Why is the length 4 instead of 3?
brain = " Know yourself! Understand yourself! Correct yourself! "
brain = brain.strip()
change = brain.split("yourself!")
print(len(change))
Because of your word that you want to split with that (yourself!) is at the end(or at the first) of your string.
you can try this solution :
def is_not_none(element):
if element is not None:
return element
brain = "know yourself! Understand yourself! Correct yourself! "
brain = brain.strip()
brain = list(filter(is_not_none, brain.rsplit('yourself!')))
print(brain)
This question already has answers here:
When are parentheses required around a tuple?
(3 answers)
Closed 3 years ago.
I am currently learning python 3. I came up with the term called "Tuple". When I have to add something to a tuple, I have to add a " , " at the end. Otherwise, it gives me an error.
a = ('alpha', 'bat', 'call', 'note')
a + ('new',) # Why I have to use that ',' after 'new'?
If you try to append a non-tuple item to a tuple you get met with the following error message
TypeError: can only concatenate tuple (not "str") to tuple
According to the Python Docs:
A single item tuple must have a trailing comma, such as (d,).
The reason for this is because tuples contain 2 items so you usually don't want to only append single items to a tuple- might as well use a list if that is your goal. If you must add a single item tuple you have to essentially construct it as if it a two item tuple and the syntax to do that in python is by adding a comma to a single item.