In: Computer Science
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
# 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