Question

Ruby Assignment - Text file format conversion: Write a program that will take a text file...

Ruby Assignment - Text file format conversion: Write a program that will take a text file with lines in this format:

Last_name:First_name:Number_of_cats:Number_of_dogs:Number_of_fish:Number_of_other_pets

and produce a text file with lines in this format:

First_name Last_name:Total_number_of_pets

For example, if the original text file looks like this:

Chawla:Kalpana:0:0:3:0
Davis:Jo Ann:0:0:5:2
de Vries:Gustav:3:1:0:1
Goldberg:Adele:2:1:1:0
Noriega:Carlos:2:2:0:0
Ride:Sally:0:1:0:2
Turing:Alan:0:0:0:0

the new text file should look like this:

Kalpana Chawla:3
Jo Ann Davis:7
Gustav de Vries:5
Adele Goldberg:4
Carlos Noriega:4
Sally Ride:3
Alan Turing:0

Note that first and last names can contain spaces. The delimiter between fields in the original file will be colon (:), not space.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code for the above problem is below:

# we loop through each line in our input text File

# i.e data.txt

File.foreach("data.txt") {

|line|

a= line.split(':') # we split the line based on : as deliminter

name=a[1]+" "+a[0] # index 1 has first name and 0 last name

# converting number to int and summing the pets

sum_pets=a[2].to_i+a[3].to_i+a[4].to_i+a[5].to_i

# writing the result in new file result.txt

File.write("result.txt", name+":"+sum_pets.to_s+"\n", mode: "a")

}

CODE SCREEN SHOT

INPUT FILE- The input file name is data.txt contains data as given in the question.

Output file- The output is stored in result.txt. you are not required to create this file it will be automatically created when the program is executed. This file is opened in append mode i.e "a". which means it will the append output at the end. So, if you run your program multiple times results will be appended multiple times in the file.

Add a comment
Know the answer?
Add Answer to:
Ruby Assignment - Text file format conversion: Write a program that will take a text file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Submit part 0, as a plain text file. Submit parts 1 - 3 as a Ruby...

    Submit part 0, as a plain text file. Submit parts 1 - 3 as a Ruby source file. Part 0 The Ruby API Information about Ruby’s regular expressions Information about Ruby’s ranges Part 1 A method that substitutes part of a string Part 2 Print the string “Hello, world.” For the string “Hello, Ruby,” find the index of the word “Ruby.”

  • Write a program that allows the user to navigate the lines of text in a file....

    Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...

  • Wk 4 - Write a Ruby Program (due Mon] Assignment Content The college IT department manager...

    Wk 4 - Write a Ruby Program (due Mon] Assignment Content The college IT department manager no longer wants to use spreadsheets to calculate grades. Instead, the manager has asked you to create a program that will input the teachers' files and output the students' grades. Write a Ruby program named format file.rb, which can be run by typing ruby widgets.rb. In your Ruby environment, the program must read an input file formatted in CSV format, named input.csv. Each record...

  • Write a C++ program that reads text from a file and encrypts the file by adding...

    Write a C++ program that reads text from a file and encrypts the file by adding 6 to the ASCII value of each character. See section 5.11 in Starting out with C++ for information on reading and writing to text files. Your program should: 1. Read the provided plain.txt file one line at a time. Because this file has spaces, use getline (see section 3.8). 2. Change each character of the string by adding 6 to it. 3. Write the...

  • JAVA Write a program that 1. opens a text file 2. reads lines of text from...

    JAVA Write a program that 1. opens a text file 2. reads lines of text from the file 3. Converts each line to uppercase 4. saves them in an ArrayList 5. Writes the list of Strings to a new file named "CAPSTEXT.txt" Mainjavo ext.txt Instructions from your teacher 1 My mistress' eyes are nothing like the sun; 2 Coral is far more red than her lips' red; 3 If snow be white, why then her breasts are dun; 4 If...

  • Lab #10 C++ Write a C++ program that reads text from a file and encrypts the...

    Lab #10 C++ Write a C++ program that reads text from a file and encrypts the file by adding an encryption factor (EF) to the ASCII value of each character. The encryption factor is 1 for the first line and increases by 1 for each line up to 4 and then starts over at 1. So, for the 4 th line the EF is 4, for the 5th line it is 1, for the 10th line it is 2. In...

  • Write a program that reads the integer numbers in a text file (numbers.txt) and stores the...

    Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0 7. Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4 5 6 7...

  • Write a program that reads the integer numbers in a text file (numbers.txt) and stores the...

    Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4 5 6 7 8 9 0 IN JAVA PLZ 7. Write a program that reads the integer numbers in a text file (numbers.txt) and stores the numbers to a binary file (binaryNumber.dat). (5 pts) The number.txt file contains the following numbers: 1 2 3 4...

  • Write a program that reverses a text file by using a stack. The user interface must...

    Write a program that reverses a text file by using a stack. The user interface must consist of 2 list boxes and 3 buttons. The 3 buttons are: Read - reads the text file into list box 1. Reverse - reverses the items in list box 1 by pushing them onto stack 1, then popping them from stack 1 (in the reverse order) and adding them to list box 2. Write - writes the contents of list box 2 to...

  • Write c program. Do part 4 and 5 CH-12 TEXT FILES SE 12-3 Create a text file named grade.txt that you type yourself by your Each record in grade.txt should have the following format: by Columns...

    Write c program. Do part 4 and 5 CH-12 TEXT FILES SE 12-3 Create a text file named grade.txt that you type yourself by your Each record in grade.txt should have the following format: by Columns 1-4 6-8 number of a student A grade out of 100 EYERCISE 12-4 Write a program that will read the identification numbers of students and their letter grades (A, B, C, D, or F) from the keyboard and store them in a text file...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT