I have a text file that looks like this:
STUFF UP HERE
APEXED NUMBER : 123456789
1234567 Bob,Hope E. 123.12
1234567 TOM ROGERS JR III 123.18
1234567 NICE, JOHNATH 4,450.00
1234567 PERDOND, DELLA 4,762.00
1234567 ERICCY, PHIL 4,552.00
STUFF IN BETWEEN
APEXED NUMBER :
1234567 RICHARDSON,FELICIA D 632.00
1234567 EARLEY, RICKY L 140.00
STUFF ON THE BOTTOM
I want to read the file and find the words "APEXED NUMBER :" Then I want to determine if there are numbers after the colon. For example after the first APEXED NUMBER : the numbers 123456789 appear. I want to save this number. Then I want the file to skip a line and read the numbers and information after - assigning the information to different variables.
Then I want to continue through the file (line by line) until I find another "APEXED NUMBER" text and check if there are numbers after it - if there are not I want to assign these APEX NUMBER a value of "unknown" and move on.
Then take all the text found and store in an array separated by commas.
Here is my current attempt:
def is_numeric?(object) #used to determine if a number is a number
true if Float(object) rescue false
end
def is_apexed_line?(object) # check if text has "APEXED NUMBER :"
true if object == "APEXED NUMBER :" rescue false
end
def load_file
raw_records = []
infile = File.open("test.txt", "r")
while line = infile.gets
possible_apexed_line = line[2,15]
if is_apexed_line?(possible_apexed_line)
apexed_line = line[2,15]
possible_apexed_number_present = line[18,9]
if is_numeric?(possible_apexed_number_present)
abc_apexed_number = line[18,9]
else abc_apexed_number = "unknown"
end # end of if
record = [apexed_line, abc_apexed_number]
raw_records << record
end # end of if
end
puts raw_records.map {|record| record*','}
infile.close
end
load_file
This produces:
APEXED NUMBER :, 123456789
APEXED NUMBER :, unknown
But this is as far as my learning thus far will take me. The result I am looking for is this:
1234567, BOB, HOPE E., 123.12, APEXED NUMBER :, 123456789
1234567, TOM ROGERS JR III, 123.18 , APEXED NUMBER :, 123456789
1234567, NICE, JOHNATH, 4450.00 ,APEXED NUMBER :, 123456789
1234567, PERDOND, DELLA, 4762.00 , APEXED NUMBER :, 123456789
1234567, ERICCY, PHIL, 4552.00, APEXED NUMBER :, 123456789
1234567, RICHARDSON,FELICIA D, 632.00 , APEXED NUMBER :, unknown
1234567, EARLEY, RICKY L, 140.00 , APEXED NUMBER :, unknown
Any suggestions/help to point me in the right direction will be appreciated. I am not wedded to this approach. If there are other ways to do it please suggest... I am learning ruby so I would prefer ruby suggestions.
Thanks