loop, insdide do loop. do twice?

  • Thread starter Bigmac Turdsplash
  • Start date
B

Bigmac Turdsplash

mply = 0
body = "just some data"
proof = 100
VALUE=""
list = ['line1','line2','line3']

foreach(list) do |block|
while mply < 3
puts mply.to_s+block+VALUE
if body.bytesize > proof
print("writing to log.html #{VAULE}")
end
mply = mply + 1
end
mply = 0
end
VALUE="XXXX"

# if you run this example above, you should see something printed like
this
# 0line1
# 1line2
# 2line3


now what im trying to do could easly be done with just doubling this
code but that is messy and i need to clean it up...

how can i produce this output and keep things short and simple
# 0line1
# 0line1XXXX
# 1line2
# 1line2XXXX
# 2line3
# 2line3XXXX

im in irc /server freenode #ruby if some one could help me or just
respond here... this one has me scratching my head for a few days...
 
R

Robert Klemme

mply =3D 0
body =3D "just some data"
proof =3D 100
VALUE=3D""
list =3D ['line1','line2','line3']

foreach(list) do |block|
while mply < 3

You can use #times for iteration.
=A0puts mply.to_s+block+VALUE
=A0 =A0if body.bytesize > proof
=A0 =A0 =A0print("writing to log.html #{VAULE}")

Note that print does not append a line terminator (as puts does).
=A0 =A0end
=A0 =A0mply =3D mply + 1
=A0end
=A0mply =3D 0
end
VALUE=3D"XXXX"

Reassigning a constant is a bad idea.
# if you run this example above, you should see something printed like
this
# 0line1
# 1line2
# 2line3


now what im trying to do could easly be done with just doubling this
code but that is messy and i need to clean it up...

how can i produce this output and keep things short and simple
# 0line1
# 0line1XXXX
# 1line2
# 1line2XXXX
# 2line3
# 2line3XXXX

im in irc /server freenode #ruby if some one could help me or just
respond here... this one has me scratching my head for a few days...

This all can be solved with nested iterations - you just need to stack
your loops appropriately: the outer loop iterates what changes most
infrequently ("list" in your example). The next inner loop must
iterated over next infrequent changing item etc. Also, you can use
#each_with_index to get the index of each element in your list.

08:57:01 ~$ ruby19 x
0line1
0line1XXX
1line2
1line2XXX
2line3
2line3XXX
09:03:24 ~$ cat x
proof =3D 100
body =3D "just some data"

%w{line1 line2 line3}.each_with_index do |block, mypl|
['', 'XXX'].each do |suffix|
puts "#{mypl}#{block}#{suffix}"
puts "Writing to log.html #{suffix}" if body.bytesize > proof
end
end
09:03:36 ~$

Cheers

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top