Issues with user input arrays


#1

I am currently working through "Learn to Program" bye Chris Pine and he is asking me to "Let's write a program which asks us to type in as many words as we want (one word per line, continuing until we just press Enter on an empty line), and which then repeats the words back to us in alphabetical order."

I haven't had any issues learning Ruby until I hit arrays and I can't figure out codeacadamy's Iterating over arrays. I'm sure my code is a mess since I'm new to this but could someone give me a clue as to how best to accomplish this?

My code:
words = []

puts "Select words to add to the array and enter a blank line to finish"

words do { |word| gets.chomp.push }
if word == ""
puts "And now in alphabetical order"
puts.sort words
end
end


#2

Is this relating to a track lesson, or your own exercise? Please post a link to the exercise. Thank you.


#3

https://pine.fm/LearnToProgram/chap_07.html

But I am also working on codeacademy Data Structures lesson 11

I can't seem to figure out arrays on either lesson


#4

Lesson 11 asks us to iterate the languages array using Array#each. The each method takes a parameter between pipes, and either a block or a do..end.

languages.each { |language| puts language }

or

languages.each do |language|
    puts language
end

#5

Here is a simple solution to building the array with inputs:

words = []
begin
  words << gets.chomp
end until words[words.length-1] == ""
words.pop

The last line removes the empty element from the end of the array.

Here is a functional approach using sort!

def word_list
  words = []
  begin
    words << gets.chomp
  end until words[words.length-1] == ""
  words.pop
  words.sort!
end
word_list().each { |word| puts word }

Notice that since the return value is an array, the last line is perfectly valid. Mind, nothing is stored. We would need to assign the return value for it to be stored:

words = word_list()
words.each do |word|
  puts word
end

The process for sorting without using a built in function I leave for you. The simplest would be a bubble sort.


#6

thank you sooooo much. I was way off but that makes great sense. One more question though:

I don't recognize the double less than operator (<<). What does that do? Even if I would have been able to write my own version that would work correctly, I wouldn't have known what that is or does.

Thank you for your help on all of this. I've already got to the end of the Data Structures lessons


#7

Array#<< is a unit append that can only take a single object. Array#push can take multiple arguments. Both do the same thing on single arguments.

It follows that input validation will make the program that much smarter.

For example...

def word_list
  words = []
  begin
    word = gets.chomp
    if word.length > 0
      words.push(word.split()[0])
    end
  end until word == ''
  words.sort!
end

words = word_list()
words.each do |word|
  puts word
end

With these inputs,

 this is
 is a
 a list
 list of
 of words
 words

The output is,

a
is
list
of
this
words
["a", "is", "list", "of", "this", "words"]

What we've done here is only push the first word when multiple are inputed. Empty string is the breaking condition.