Welcome to Dream.In.Code
Click Here
Getting Help is Easy!

Join 118,658 Programmers for FREE! Ask your question and get quick answers from experts. There are 878 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



i need help with arrays...

 
Reply to this topicStart new topic

i need help with arrays..., Ruby

JohnMaddix16
post 14 Jun, 2007 - 01:02 AM
Post #1


New D.I.C Head

*
Joined: 14 Jun, 2007
Posts: 3


My Contributions


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/LearnToProgram/?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. ><)
User is offlineProfile CardPM

Go to the top of the page


rahulbatra
post 14 Jun, 2007 - 03:44 AM
Post #2


D.I.C Head

Group Icon
Joined: 28 Dec, 2005
Posts: 156



Dream Kudos: 275
My Contributions


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,

CODE
arrName = []


where 'arrName' is the array name. Now a push method would insert/push a new value into the array. For example,

CODE
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.
User is offlineProfile CardPM

Go to the top of the page

JohnMaddix16
post 14 Jun, 2007 - 10:47 AM
Post #3


New D.I.C Head

*
Joined: 14 Jun, 2007
Posts: 3


My Contributions


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.

CODE

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.
User is offlineProfile CardPM

Go to the top of the page

JohnMaddix16
post 14 Jun, 2007 - 12:30 PM
Post #4


New D.I.C Head

*
Joined: 14 Jun, 2007
Posts: 3


My Contributions


haha!
Nevermind, i stripped it all down, and rebuilt it.
I realized what i was doing wrong.

CODE

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
: ]
User is offlineProfile CardPM

Go to the top of the page

Mujklob
post 1 Aug, 2007 - 03:45 PM
Post #5


New D.I.C Head

*
Joined: 1 Aug, 2007
Posts: 1



Thanked 1 times
My Contributions


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


This post has been edited by Mujklob: 1 Aug, 2007 - 03:49 PM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 10/12/08 03:42AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month