Question

In: Computer Science

For this problem, write a Ruby script named arr2cols.rb that prints an array’s contents as formatted...

For this problem, write a Ruby script named arr2cols.rb that prints an array’s contents as formatted rows and columns. The data is provided by the predefined arrays below: escargot_player_data, employees, andartists. Your script must work with all three arrays.

The number of columns in the data will be consistent within each data set, but each dataset will have a different number of columns. We can the use Array#each method to iterate all of the elements in an array. each will give us each element in the order it appears in the array.

Your script should work with all these three arrays of data: escargot_player_data, employees, and artists.

Array 1: Escargot Players Data The escargot_player data is a multidimensional array with each sub-array containing three strings.

# A array containing Escargot player data
escargot_player_data = [
  # Column names: name, character, and points
  ['Jim','bullfrog',99],
  ['Mack the Knife','caterpillar',12],
  ['Willy','chihuahua',143],
  ['Trudy','bunny',3],
  ['Mary Lou','slow loris',1443],
  ['Sharon Stone','komodo dragon',8888],
]

Array 2: Employee data The employees array is a multidimensional array containing rows with six elements.

employees = [
    #  Name Address City State SSN Telephone
    %w(Walter\ White 123\ Happy\ Home\ Drive Albuquerque NM 555-66-7777 505-123-4567 ),
    %w(Jesse\ Pinkman 43\ Cloudy\ Skies\ Parkway Albuquerque NM  666-12-3456, 505-888-9999 ),
    %w(Gustavo\ Fring 123\ Pollos\ Boulevard Albuquerque NM 565-32-3344 505-434-9001 ),
    %w(Tuco\ Salamanca 99\ Crystal\ Springs\ Lane Albuquerque NM 575-44-3553 505-776-0455 ),
    %w(Saul\ Goodman 9800\ Montgomery\ Blvd\ NE Albuquerque NM 585-19-9990 505-503-4455 )
]

Array 3: Artist Addresses and Income The artists array is a multidimensional array with 9 elements in each sub array.

artists = [
   # column names
   %w( first_name last_name telephone address city state zip_code birthdate salary ),

   [ 'Vinh',
     'Tranh',
     '438-910-7449',
     '8235 Maple Street',
     'Wilmington',
     'VM',
     '29085',
     '9/23/63',
     '1200'
   ],

   [ 'William',
     'Kopf',
     '846-836-2837',
     '6937 Ware Road',
     'Milton',
     'PA',
     '93756',
     '9/21/46',
     '43500'
   ],

   [ 'Yukio',
     'Takeshida',
     '387-827-1095',
     '13 Uno Lane',
     'Ashville',
     'NC',
     '23556',
     '7/1/29',
     '57000'
   ],

   [ 'Zippy',
     'Pinhead',
     '834-823-8319',
     '2356 Bizarro Ave.',
     'Farmount',
     'IL',
     '84357',
     '1/1/67',
     '89500'
   ],
   [ 'Andy',
     'Warhol',
     '212-321-7654',
     '231 East 47th Street',
     'New York City',
     'NY',
     '10017',
     '8/6/1928',
     '2700000' 
   ]
]

3.1 arr2cols.rb code to get you started

# Start building the table by concatenation
#!/usr/local/bin/ruby
# Name: Your Name
# File: arr2cols.rb
# Desc: Uses Array methods to manipulate an array created from a string
#       Example tables: escargot_player_data, employees, and artists 

# 1. Code from Lab 1 to fetch the_string from a URL
# 2. Add the pre-defined arrays here.

all_arrays = [ escargot_player_data, employees, artists ]

# Iterate through the array of players one row at a time
all_arrays.each do |array|

  # Print everything in columns

end

Solutions

Expert Solution

# Program:

escargot_player_data = [
# Column names: name, character, and points
['Jim','bullfrog',99],
['Mack the Knife','caterpillar',12],
['Willy','chihuahua',143],
['Trudy','bunny',3],
['Mary Lou','slow loris',1443],
['Sharon Stone','komodo dragon',8888],
]

employees = [
# Name Address City State SSN Telephone
%w(Walter\ White 123\ Happy\ Home\ Drive Albuquerque NM 555-66-7777 505-123-

4567 ),
%w(Jesse\ Pinkman 43\ Cloudy\ Skies\ Parkway Albuquerque NM 666-12-3456, 505-

888-9999 ),
%w(Gustavo\ Fring 123\ Pollos\ Boulevard Albuquerque NM 565-32-3344 505-434-

9001 ),
%w(Tuco\ Salamanca 99\ Crystal\ Springs\ Lane Albuquerque NM 575-44-3553 505-

776-0455 ),
%w(Saul\ Goodman 9800\ Montgomery\ Blvd\ NE Albuquerque NM 585-19-9990 505-503

-4455 )
]

artists = [
# column names
%w( first_name last_name telephone address city state zip_code birthdate salary

),

[ 'Vinh',
'Tranh',
'438-910-7449',
'8235 Maple Street',
'Wilmington',
'VM',
'29085',
'9/23/63',
'1200'
],

[ 'William',
'Kopf',
'846-836-2837',
'6937 Ware Road',
'Milton',
'PA',
'93756',
'9/21/46',
'43500'
],

[ 'Yukio',
'Takeshida',
'387-827-1095',
'13 Uno Lane',
'Ashville',
'NC',
'23556',
'7/1/29',
'57000'
],

[ 'Zippy',
'Pinhead',
'834-823-8319',
'2356 Bizarro Ave.',
'Farmount',
'IL',
'84357',
'1/1/67',
'89500'
],
[ 'Andy',
'Warhol',
'212-321-7654',
'231 East 47th Street',
'New York City',
'NY',
'10017',
'8/6/1928',
'2700000'
]
]

all_arrays = [ escargot_player_data, employees, artists ]
all_arrays.each do |array| # Accessing 2-D Array from all_arrays
   for a in array # Accessing Each array from 2-D Array
       puts "*******ROW*******"
       for ad in a # Accessing Each Row from each Array
           puts ad # Printing Each Column from Each Row from Each Array from 2-D Array
       end
   end
end

Output:

#The below is the Login for only print players_data instead of all arrays

all_arrays = [ escargot_player_data, employees, artists ]
i=0
all_arrays.each do |array|
   i=i+1
   for a in array
       puts "*******ROW*******"
       for ad in a
           puts ad
       end
   end
   break if i==1
end

We can use "next iterative statement" for going to continue next arrays i.e employees and artists


Related Solutions

write a script in ruby to automate user account creation in Linux using a CSV file....
write a script in ruby to automate user account creation in Linux using a CSV file. Username is a combination of first initial last initial of the first name and last name in the last name followed by the first initial last initial(Ex. Sam Smith = smithsm). If two or more employees had the same first and last name appends a number to the end of the username. After accounts are created write the first and last names along with...
Write a script named numberlines.py. This script creates a program listing from a source program. This...
Write a script named numberlines.py. This script creates a program listing from a source program. This script should: Prompt the user for the names of two files. The input filename could be the name of the script itself, but be careful to use a different output filename! The script copies the lines of text from the input file to the output file, numbering each line as it goes. The line numbers should be right-justified in 4 columns, so that the...
write a script named compute.sh that is used to do simple arithmetic for the user. There...
write a script named compute.sh that is used to do simple arithmetic for the user. There should be no command line arguments. Instead, all values from the user should be prompted for and read in to the script using the read command. Specifically, you need to ask the user for two integers and an operation string. The operation should be "add", "sub", "mul", "div", or "exp", for addition, subtraction, multiplication, division, and exponentiation, respectively. Your script should take the two...
C++ 10.15: Character Analysis Write a program that reads the contents of a file named text.txt...
C++ 10.15: Character Analysis Write a program that reads the contents of a file named text.txt and determines the following: The number of uppercase letters in the file The number of lowercase letters in the file The number of digits in the file Prompts And Output Labels. There are no prompts-- nothing is read from standard in, just from the file text.txt. Each of the numbers calculated is displayed on a separate line on standard output, preceded by the following...
Write a bash script named q1f.sh that will edit the PATH environment variable to include the...
Write a bash script named q1f.sh that will edit the PATH environment variable to include the sourcefiles directory in your home directory and make the new variable global. Please show picture of output.
Write a script named countmatches that expects at least two arguments on the command line. The...
Write a script named countmatches that expects at least two arguments on the command line. The first argument is the pathname of a dna file containing a valid DNA string with no newline characters or white space characters of any kind within it. (It will be terminated with a newline character.) This dna file contains nothing but a sequence of the bases a, c, g, and t in any order. The remaining arguments are strings containing only the bases a,...
Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the...
Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the result. If no parameters passed, prompt a user (only one time) to enter numbers to sum and print the result. Submit your program as LastName_FirstName.sh file.
Write a C program that, given a file named Program_2.dat as input, determines and prints the...
Write a C program that, given a file named Program_2.dat as input, determines and prints the following information: The number of characters in the file. The number of uppercase letters in the file. The number of lowercase letters in the file. The number of words in the file. The number of lines in the file. Your program should assume that the input file, Program_2.dat, may contain any text whatsoever, and that text might be, or might not be, the excerpt...
Write a script that creates and calls a stored procedure named test. This procedure should identify...
Write a script that creates and calls a stored procedure named test. This procedure should identify all of the prime numbers less than 100. (A prime number is an integer that can't be divided by another integer other than 1 and itself.) Then, it should display a string variable that includes the prime numbers like this: 2 1 3 1 5 1 7 1 1 1 1 1 3 1 1 7 1 1 9 1 2 3 1 2...
SQL Code: Write a script that creates and calls a stored procedure named test. This procedure...
SQL Code: Write a script that creates and calls a stored procedure named test. This procedure should identify all of the prime numbers less than 100. (A prime number is an integer that can't be divided by another integer other than 1 and itself.) Then, it should display a string variable that includes the prime numbers like this: 2 1 3 1 5 1 7 1 1 1 1 1 3 1 1 7 1 1 9 1 2 3...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT