Welcome to Dream.In.Code
Become an Expert!

Join 136,940 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,865 people online right now. 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
14 Jun, 2007 - 12: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
+Quote Post

rahulbatra
RE: I Need Help With Arrays...
14 Jun, 2007 - 02:44 AM
Post #2

D.I.C Head
Group Icon

Joined: 28 Dec, 2005
Posts: 162


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
+Quote Post

JohnMaddix16
RE: I Need Help With Arrays...
14 Jun, 2007 - 09: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
+Quote Post

JohnMaddix16
RE: I Need Help With Arrays...
14 Jun, 2007 - 11:30 AM
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
+Quote Post

Mujklob
RE: I Need Help With Arrays...
1 Aug, 2007 - 02: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 - 02:49 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 10:55PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month