LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-07-2006, 10:18 PM   #1
lmcilwain
Member
 
Registered: Dec 2003
Location: Maryland
Distribution: Fedora, Ubuntu, Centos, FreeBSD
Posts: 390

Rep: Reputation: 31
[ruby newbie] Cycling through arrays


Hello all,

I am trying to write a program using ruby, that notifies individuals of certain things that they have been selected for. The way that I am writing this is by using 3 arrays to store the following:
1. dates
2. names
3. email addresses

I then want to merge each record of the array into pre-defined text. Eventally I will want to get this to the point where I can send this via e mail once its done but a little at a time

note:
I do understand that the program is probably not written in the best way but I am just learning so I am not looking to take the shortest route, just to make a functional program, then to shorten it later as I understand things.

program (example)

names_array = []
emails_array = []
dates_array =[]

puts 'Enter name(s) of the person(s) that will be having the day off'
name_input = gets.chomp
while (name_input != '')
names_array [names_array.length] = name_input
name_input = gets.chomp
end

puts 'Enter the email address(es) of the person(s) that will be having the day off'
email_input = gets.chomp
while (email_input != '')
emails_array[emails_array.length] = email_input
email_input = gets.chomp
end

puts 'Enter the date the person who will be having the day off'
date_input = gets.chomp.to_s
while (date_input != '')
dates_array[dates_array.length] = date_input
date_input = gets.chomp
end

while (?) # Can't seem to figure this part out

puts 'Dear ' + names_array[0] # Can't seem to figure this part out.
puty ''
puts 'You have been selected to win a day off on ' +dates_array[0].to_s+ '. You will have a full day of doing nothing and relaxing as you see fit. If you have any questions, please feel free to contact me at my email address user@isp.com.'
puty ''
puty ''
puts 'Thanks'
end
 
Old 11-08-2006, 03:18 PM   #2
taylor_venable
Member
 
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
First make sure the lengths of all three arrays are the same. Then do an iterative loop using a variable that covers the range 0 .. name_array.length - 1; use this as the index into the array to determine the person's name / email / date. Simple, but not really relying on the power of Ruby.
 
Old 11-08-2006, 04:09 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Or def a class with touple you need, and just maintain one array. Then use .each to iterate through the array. Parrallel arrays in any language is a bad idea.

Warning: I am a Ruby newbie
 
Old 11-08-2006, 08:51 PM   #4
lmcilwain
Member
 
Registered: Dec 2003
Location: Maryland
Distribution: Fedora, Ubuntu, Centos, FreeBSD
Posts: 390

Original Poster
Rep: Reputation: 31
I ended up looping on the names_array index and doing an itteration on that for all of the other arrays. Had to look through documentation and after an hour finally got it right. For others that want to see its below:

===================
#set arrays
names_array = []
emails_array = []
dates_array =[]

#populate arrays
persons_name = gets.chomp
names_array[names_array.length] = persons_name

persons_email = gets.chomp
emails_array[emails_array.length] = persons_email

date_of_vacation = gets.chomp
dates_array[dates_array.length] = date_of_vacation


names_array.each_index do |index|

#Text Iterating through the arrays
puts 'Dear ' + names_array[index].to_s + ','
puts ''
puts 'You have been selected to win a day off on ' +dates_array[index].to_s+ '. You will have a full day of doing nothing and relaxing as you see fit. If you have any questions, please feel free to contact me at my email address user@isp.com.'
puty ''
puty ''
puts 'Thanks'
end
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Newbie: Ruby and Writing Variables In Strings lmcilwain Programming 4 10-18-2006 01:19 PM
KDE Kicker and Application "Cycling" With the Mouse Woodsman Slackware 1 09-20-2006 12:01 PM
Portage and Ruby Ephracis Programming 3 09-15-2006 08:40 AM
Arrays - User input - newbie = [ ohfaney Programming 6 05-01-2006 10:14 PM
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 05:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration