I have a set of fields to parse from a file and Im doing it line by line inside a foreach loop, i want to know how i can skip a line and go to the next line
For example : if encounter a string called “ABC”, i need to grab a number in the next line,
some characters "ABC" 123
The problem is I’m actually having a lot of numbers in the file but i need to grab a number, specifically the number which is after a line break after the string “ABC”.
How can i do this
?
It’s a bit easier to do with a
whileloop, reading one line at a time, since you can then easily read an extra line when you find your trigger case (assuming you don’t have a run of lines with"ABC"in them):The reason this is awkward with
foreachis that it will always process the same number of elements each time through the loop.If you’re dealing with data which can have the run-of-lines issue alluded to above, you are actually better off with
foreachcuriously enough:This works because when you do
lindexof something past the end, it produces the empty string (which won’t match that simple regular expression).