Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8876679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:16:13+00:00 2026-06-14T19:16:13+00:00

I have a set of fields to parse from a file and Im doing

  • 0

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
?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:16:15+00:00Added an answer on June 14, 2026 at 7:16 pm

    It’s a bit easier to do with a while loop, 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):

    set fd [open $theFilename]
    while {[gets $fd line] >= 0} {
        if {
            [string match *"ABC"* $line]
            && [gets $fd line] >= 0
            && [regexp {\d+} $line -> num]
        } then { # I like to use 'then' after a multi-line conditional; it's optional
            puts "Found number $num after \"ABC\""
        }
    }
    close $fd
    

    The reason this is awkward with foreach is 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 foreach curiously enough:

    set fd [open $theFilename]
    set lines [split [read $fd] \n]
    close $fd
    foreach line $lines {
        incr idx; # Always the index of the *next* line
        if {
            [string match *"ABC"* $line]
            && [regexp {\d+} [lindex $lines $idx] -> num]
        } then {
            puts "Found number $num after \"ABC\""
        }
    }
    

    This works because when you do lindex of something past the end, it produces the empty string (which won’t match that simple regular expression).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Service that downloads a file from the internet. What I want
I want to use reflection to set some fields according to data from a
I have a Firstname and Lastname fields, I have set rules to 'required' but
I have some markup for a set of fields within a form: <ul> <li>
I have a set of data that contains garbled text fields because of encoding
I have an MVC application which has a set of fields for contact details
I have a set of data, and while the number of fields and tables
I have a gridivew with auto-generated fields, whenever there is a new set of
I have a MySQL table & fields that are all set to UTF-8. The
I have a class that parses in data from a comma delimited text file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.