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 6593059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:38:02+00:00 2026-05-25T17:38:02+00:00

I have some code that reads a file into an array of lines, and

  • 0

I have some code that reads a file into an array of lines, and then parses those lines to get at the structured data. The input file has various different data types that need to be handled differently, additionally there are major sections for various accounts (mobile numbers).

I loop through the lines looking for the account line, identify the account, and then I want to use that account until I encounter the next account line. The lines in between potentially represent various types of data belonging to that account. The problem is, after I find the account line and set a local variable (cur_num), the variable is set to nil when I want to use it. Why, how is this happening? I am learning Ruby so I want more than a fix – I want to understand why it works this way.

Here is my code:

  count = 0
  cur_num = ""
  lines.each do |line|
  unless (line.strip.eql?(""))  # edited due to comment from normalocity
    if (line.slice(0,15) == "Mobile Number:,")
      cur_num = line.slice(15,12)
      count = 1
      puts "Current Number: #{cur_num}"
      #puts "Object Type: #{cur_num.class}"
    else 
      data = line.strip.split(',')
      if (data.length > 8)
        data.unshift(cur_num)
        #if (count.modulo(10) == 0 || count == 1)
          puts "[#{cur_num}] #{data.inspect}"
          #pp data
        #end
        count += 1
      end
    end
  end
end

An overview of the input data structure would look like this:

Account 1
    Data Section A
        data line 1
        data line 2
     Data Section B
        data line 1
        data line 2
Account 2
    Data Section A
        data line 1
        data line 2
     Data Section B
        data line 1
        data line 2

end

adding code to duplicate lines array you should paste this above code sample if you are attempting to duplicate. I’m putting it here at the end to try to make my question more readable:

lines = []
lines.push("ATT Wireless Bill")
lines.push("")
lines.push("Mobile Number:,770-555-1212")
lines.push("item,date,time,number called,rate period,plan type,minutes,airtime charge,ld charge,total charge")
lines.push("")
lines.push("1,2011-01-02,6:56AM,404-555-1212,NW,UNW,4,0.00,0.00,0.00")
lines.push("")
lines.push("2,2011-01-03,6:56AM,404-555-1212,NW,UNW,4,0.00,0.00,0.00")
lines.push("")
lines.push("1,2011-01-03,7:56AM,404-555-1213,DT,UM2M,5,0.00,0.00,0.00")
lines.push("")
lines.push("Mobile Number:,770-555-1213")
lines.push("item,date,time,number called,rate period,plan type,minutes,airtime charge,ld charge,total charge")
lines.push("")
lines.push("1,2011-01-02,6:56AM,404-555-1212,NW,UNW,4,0.00,0.00,0.00")
lines.push("")
lines.push("2,2011-01-03,6:56AM,404-555-1212,NW,UNW,4,0.00,0.00,0.00")
lines.push("")
lines.push("1,2011-01-03,7:56AM,404-555-1213,DT,UM2M,5,0.00,0.00,0.00")
lines.push("")
  • 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-05-25T17:38:02+00:00Added an answer on May 25, 2026 at 5:38 pm

    Answer to the answer Why / How is my variable getting set to nil

    Without cur_num = "" you don’t initialize cur_num outside the loop (lines.each).
    So cur_num is initialized in each loop.

    I would expect a undefined local variable or method error, but it seems cur_num is created in each loop, even, if the if-branch is not executed. So you have cur_num, but without a value (or better: it is nil).

    I added a short example to show, that a variable is created, even if the code is not executed:

    if false
      a = 1
    else
      p a #works fine, a was created, but it is nil
      p b #undefined local variable or method `b' 
    end
    

    Addendum II:

    The following code shows, that a variable inside a loop is ‘loop internal’. The 2nd (and following) cycle(s) starts without the variable a.

    #~ a = 0  # uncomment to compare
    5.times{
      if defined? a
        puts "a is defined as #{a.inspect}"
      else
        puts "a is not defined"
        a = 1 #define it now
      end
    }
    

    Related questions: The scope is confusing


    I redesigned your code a bit. When I parse texts like yours, I prefer to use a case-statement with regular expressions:

    count = 0
    cur_num = ""
    DATA.each_line do |line|
      case line
        when /\A\s\Z*/ #skip empty lines
        when /Mobile Number:,(.{12})/
          cur_num = $1
          count = 1
          puts "Current Number: #{cur_num}"
          #puts "Object Type: #{cur_num.class}"
        else 
          data = line.strip.split(',')
          if data.length > 8
            data.unshift(cur_num)
            #if (count.modulo(10) == 0 || count == 1)
              puts "[#{cur_num}] #{data.inspect}"
              #pp data
            #end
            count += 1
          end #(data.length > 8)
      end #case line
    end
    
    __END__
    ATT Wireless Bill
    
    Mobile Number:,770-555-1212
    item,date,time,number called,rate period,plan type,minutes,airtime charge,ld charge,total charge
    
    1,2011-01-02,6:56AM,404-555-1212,NW,UNW,4,0.00,0.00,0.00
    
    2,2011-01-03,6:56AM,404-555-1212,NW,UNW,4,0.00,0.00,0.00
    
    1,2011-01-03,7:56AM,404-555-1213,DT,UM2M,5,0.00,0.00,0.00
    
    Mobile Number:,770-555-1213
    item,date,time,number called,rate period,plan type,minutes,airtime charge,ld charge,total charge
    
    1,2011-01-02,6:56AM,404-555-1212,NW,UNW,4,0.00,0.00,0.00
    
    2,2011-01-03,6:56AM,404-555-1212,NW,UNW,4,0.00,0.00,0.00
    
    1,2011-01-03,7:56AM,404-555-1213,DT,UM2M,5,0.00,0.00,0.00
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code that gives a user id to a utility that then
I have some code that effectively does this : File file = new File(C:\\Program
I have some code that prints out databse values into a repeater control on
I have some code that uses the shared gateway pattern to implement an inversion
I have some code that raises PropertyChanged events and I would like to be
I have some code that looks like: template<unsigned int A, unsigned int B> int
I have some code that generates image of a pie chart. It's a general
I have some code that I am putting in the code-behind of a master
I have some code that uses the Oracle function add_months to increment a Date
I have some code that produces a set of primary key values that I

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.