Noob question: while or for loop within a list


#1

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?


#2

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


#3

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


#4

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.


#5

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


#6

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 }

www.yahoo.com
www.google.com
www.ruby-lang.com


#7

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?


#8

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


#9

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


#10

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