IT Community - Software Programming, Web Development and Technical Support

how to assign info to a blank array?

This is a discussion on how to assign info to a blank array? within the Ruby forums, part of the Web Development category; how to assign info to a blank array...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Ruby

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 10-12-2007, 11:22 PM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Question how to assign info to a blank array?

how to assign info to a blank array
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-16-2007, 04:32 AM
mobilegeek mobilegeek is offline
D-Web Analyst
 
Join Date: Jun 2007
Posts: 205
mobilegeek is on a distinguished road
Smile Re: how to assign info to a blank array?

You can either declare an array using Array.new, or assign it to an empty array instance []:

Code:
words = Array.new
or

Code:
words = []
As for the rest of the code, it is not too hard to write


Code:
#!/usr/bin/env ruby

words = Array.new

# Get list of words from the user
word = gets.chomp
while word != ''
  words.push word
  word = gets.chomp
end

# Sort the array
sortedwords = words.sort

# Print it out
sortedwords.each do |word|
  puts word
end
and if you don't want to declare a separate sorted array, you can use words.sort! instead (note the ! at the end of the function name. In ruby, functions ending with ! end up modifying the object directly, instead of returning a copy)

Code:
#!/usr/bin/env ruby

words = Array.new

# Get list of words from the user
word = gets.chomp
while word != ''
  words.push word
  word = gets.chomp
end

# Sort the array
words.sort!

# Print it out
words.each do |word|
  puts word
end
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 11-20-2007, 01:48 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Cool Re: how to assign info to a blank array?

Hey buddy...!


Thanks alot!

And also thanks alot for explaining it to me, i really appreciate you spending the time to do it.
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to assign html Content to RichText as a Normal Text? poornima ASP and ASP.NET Programming 3 03-10-2008 02:15 AM
What’s the difference between the System.Array.CopyTo() and System.Array.Clone() ? Archer C# Programming 2 08-25-2007 02:00 AM
When to you use $_REQUEST[ ] array in php..? oxygen PHP Programming 2 07-30-2007 03:52 AM
Array filtering Jeyaseelansarc PHP Programming 1 07-17-2007 08:07 AM
Paging array in PHP kingmaker PHP Programming 0 07-16-2007 10:11 PM


All times are GMT -7. The time now is 08:42 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0