Noob question: while or for loop within a list

Essentially I want to do the following:
list = “url1, irl2, url3, url4, url5”
while [ list ]; #in other words, we loop 5 times or = # things in list
do
#A Bunch of Stuff 1
#A Bunch of Stuff 2
done

My list is essentially a list of URLs. I’m most familiar with doing it
in a text list, but I guess I could create an array, count that array,
and then do a loop with a counter.

Suggestions / Best practices?

On [Tue, 03.02.2009 04:14], Darin Ginther wrote:

Essentially I want to do the following:
list = “url1, irl2, url3, url4, url5”
while [ list ]; #in other words, we loop 5 times or = # things in list
do
#A Bunch of Stuff 1
#A Bunch of Stuff 2
done

[snipp]

Suggestions / Best practices?

list.split(", ").each do |url|

do fancy stuff with our url

end

How do I access the individual element of the split?
IE:

For $i in [list];
do
#A Bunch of Stuff 1 + $i
#A Bunch of Stuff 2 + $i
done
exit

My code does exactly what you want.

.each is iterating over each item and stores it in the variable “url”.
So instead of $i you got url.

And you should read some basic stuff about Ruby, like the Pickaxe, to
get started.

Darin Ginther wrote:

list = “A,B,C,D”
list.split(", ").each do |url|
puts “URL IS:”+url
end

Output: URL IS:A,B,C,D

Is using an array a “more correct” way to do it in Ruby?

Your first example began with

list = “url1, irl2, url3, url4, url5”

but now your list looks like this:

list = “A,B,C,D”

The spaces are missing, so splitting on a comma followed by a space does
not split anything in your second example. In your first example, it
produces an array with 4 elements. In your second list it produces an
array with one elemement: “A,B,C,D”

hth,

Siep

Darin Ginther wrote:

list = “A,B,C,D”
list.split(", ").each do |url|
puts “URL IS:”+url
end

Output: URL IS:A,B,C,D

Is using an array a “more correct” way to do it in Ruby?

The easy way…

l = %w{ www.yahoo.com www.google.com www.ruby-lang.com }

l.each { |e| puts e }

list = “A,B,C,D”
list.split(", ").each do |url|
puts “URL IS:”+url
end

Output: URL IS:A,B,C,D

Is using an array a “more correct” way to do it in Ruby?

On 02.02.2009 20:40, Darin Ginther wrote:

list = “A,B,C,D”
list.split(", ").each do |url|
puts “URL IS:”+url
end

Output: URL IS:A,B,C,D

Is using an array a “more correct” way to do it in Ruby?

Yes. You can use the word modifier for easier typing:

urls = %w{http://foo.bar http://www.google.gov}

Now you can do

puts urls

urls.each do |url|
puts “URL is #{url}”
end

If you want to do URL manipulations check out class URI.
http://www.ruby-doc.org/core/classes/URI.html

Kind regards

robert

Thank you guys.
Obviously I need to be more aware of my white space in Ruby. I’ve put
the two examples below - both one via a list and one via an array.

List

list = “url1, url2, url3, url4, url5”
list.split(", ").each do |url|
puts url
end

#Array
urls = %w{http://test1.com http://test2.com}
urls.each do |url|
puts “URL is #{url}”
end

Darin,

Don’t let white space get you down…
list.split(/\s*,\s*/).each do

ilan

Darin Ginther wrote:

Thank you guys.
Obviously I need to be more aware of my white space in Ruby. I’ve put
the two examples below - both one via a list and one via an array.

List

list = “url1, url2, url3, url4, url5”
list.split(", ").each do |url|
puts url
end

#Array
urls = %w{http://test1.com http://test2.com}
urls.each do |url|
puts “URL is #{url}”
end