Hello!
Hi, im having problems with "arrays".
I know how to access the info inside an array and whatnot but what im having problems with is i dont know how to assign info to a blank array.
Im taking the Tutorials from
http://www.pine.fm/L...ram/?Chapter=07
And on the very bottom of the tutorial it suggest that i create a program that would let you type in words (one per line), then when a blank space is entered it would list all the words you had put in but in alphabetical order.
The only problems im having is with creating the blank array and then having the input data put into the blank array . lol
I know in order to do this i know i have to use the .push method, but im just confused and wondering if someone could explain what to do, OR just show me a working code.
Thank you!
(oh also, if you havent guessed im a programming noob. ><)
i need help with arrays...Ruby
Page 1 of 1
4 Replies - 5878 Views - Last Post: 01 August 2007 - 03:45 PM
Replies To: i need help with arrays...
#2
Re: i need help with arrays...
Posted 14 June 2007 - 03:44 AM
Ok, an array is like a list and the 'push' method (true to its name), pushes a value in the list. Your first task would be to create a blank array. In ruby this would be done by,
where 'arrName' is the array name. Now a push method would insert/push a new value into the array. For example,
This would take your blank array and put one element in it, which is the string 'Holy Cow'. The 'pop' method would do the exact opposite by removing the element from the array list. Try out the full program, and post any errors or problems you face further on.
arrName = []
where 'arrName' is the array name. Now a push method would insert/push a new value into the array. For example,
arrName.push 'Holy Cow'
This would take your blank array and put one element in it, which is the string 'Holy Cow'. The 'pop' method would do the exact opposite by removing the element from the array list. Try out the full program, and post any errors or problems you face further on.
#3
Re: i need help with arrays...
Posted 14 June 2007 - 10:47 AM
okay try not to laugh
><
Heres what i have so far, i just want a simple program that i can put input into (into the array), and then when i click enter on an empty space, it would tell me what i had input into the array.
as you can see, all it does right now is take the data and puts it in the arrat but when i press enter on a blank line it doesnt show me the array.
><
Heres what i have so far, i just want a simple program that i can put input into (into the array), and then when i click enter on an empty space, it would tell me what i had input into the array.
arrName= [] arrName=gets.chomp if puts arrName else arrName = gets.chomp end while arrName !='' arrName=gets.chomp end puts arrName
as you can see, all it does right now is take the data and puts it in the arrat but when i press enter on a blank line it doesnt show me the array.
#4
Re: i need help with arrays...
Posted 14 June 2007 - 12:30 PM
haha!
Nevermind, i stripped it all down, and rebuilt it.
I realized what i was doing wrong.
Thanks for the help tho
: ]
Nevermind, i stripped it all down, and rebuilt it.
I realized what i was doing wrong.
words = Array.new word = gets.chomp while word != '' words.push word.downcase# Rigth her i was putting words.push=word.downcase word = gets.chomp end sortedwords = words.sort puts word end
Thanks for the help tho
: ]
#13
Re: i need help with arrays...
Posted 01 August 2007 - 03:45 PM
This assignment is fairly easy. When I first tried it, I kept over complicating it until I figured out the program can be done in three lines. This is all you need to make it work.
Quote
a = []
a.push gets.chomp while a.last != ''
puts a.sort
a.push gets.chomp while a.last != ''
puts a.sort
This post has been edited by Mujklob: 01 August 2007 - 03:49 PM
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|