Noob question: while or for loop within a list

D

Darin Ginther

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?
 
D

Darin Ginther

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

For $i in
  • ;
    do
    #A Bunch of Stuff 1 + $i
    #A Bunch of Stuff 2 + $i
    done
    exit
 
D

Darin Ginther

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?
 
S

Siep Korteling

Darin said:
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
 
R

Robert Klemme

D

Darin Ginther

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
 
I

Ilan Berci

Darin,

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

ilan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top