4 Replies - 6530 Views - Last Post: 01 August 2007 - 03:45 PM Rate Topic: -----

#1 JohnMaddix16  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 14-June 07

i need help with arrays...

Posted 14 June 2007 - 01:02 AM

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. ><)

Is This A Good Question/Topic? 0
  • +

Replies To: i need help with arrays...

#2 rahulbatra  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 16
  • View blog
  • Posts: 183
  • Joined: 28-December 05

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,

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.
Was This Post Helpful? 0
  • +
  • -

#3 JohnMaddix16  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 14-June 07

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.

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.
Was This Post Helpful? 0
  • +
  • -

#4 JohnMaddix16  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 14-June 07

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.

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
: ]
Was This Post Helpful? 1

#13 Mujklob  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 1
  • Joined: 01-August 07

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

This post has been edited by Mujklob: 01 August 2007 - 03:49 PM

Was This Post Helpful? 1

Page 1 of 1