loops

for vs each in Ruby

I just had a quick question regarding loops in Ruby. Is there a difference between these two ways of iterating through a collection? # way 1 @collection.each do |item| # do whatever end # way 2 for item in @collection # do whatever end Just wondering if these are exactly the same or if maybe there's a subtle difference (possibly ...

Function with loop calling itself

I have a function, in which there is a loop which calls up the function. function displayItem(item, isChild) { if (isChild) { writeOutput('<li>' & item.name & '</li>'); } else { writeOutput('<li>' & item.name); } try { if (item.hasChild) { writeOutput('<ul>'); ...

for loop to while loop + additional condition

I have a loop similar to this. int total1, total2; for (total1 = fsize(myfile);;) { total2 = fsize(myfile); ... ... total1 = total2; } What I would like to do is to convert this to a while loop and check for an extra condition before ending the loop. I would like to do something like this: while((total1 = fsize(myfi...

Div ID fade code

Hi, I'm a newbie on jQuery. Can somebody help me with my 2days problem. I just in need of a sample code for a fading effect, slideshow. Here's the style. This will be 3 Divs with same class The first div, will show for 6secs then fades out for 2secs before the first fades out the second div will fade in for 2secs this again will sho...

How can I modify strings using a "For Each" loop?

'Why doesn't this work? Dim myStrings As String() = New String() {string1, string2, string3,} For Each s As String In myStrings If String.IsNullOrEmpty(s) Then s = "" End If s = "-" & s.Trim() & "-" Next If string1 contains "foo", my intention is that string1 contains "-foo-" aft...

Converting tree-like object structure into an array and interating the whole thing

Hi. I have a tree-like object structure that consists of two types of objects: object of class Category object of class CategoryLink The structure is the following: The whole story begins with an array of Categories that have no parent Each Category has a few unimportant properties and a few important: $parent - containing an id of ...

while(list($key, $value) = each($array)) vs. foreach($array as $key => $value)?

Recently I experienced this weird problem: while(list($key, $value) = each($array)) was not listing all array values, where replacing it with... foreach($array as $key => $value) ...worked perfectly. And, I'm curious now.. what is the difference between those two? ...

Monotonify (list-munging) without a For loop.

For or While loops in Mathematica code always make me feel a little dirty but I was confusing myself trying to do some list munging all functional-like, and resorted to this: (* # Given a list of {x,y} pairs, transform the data as follows: every time # there's a decrease in y-value from one datapoint to the next, say {x1,Y} # fo...

How do I loop through a JSON object that contains arrays?

How would I loop through this correctly? {names:['oscar','bill','brad'],ages:['20','25','18']} So i'd basically get the output: names: oscar ages: 20 names: bill ages: 25 names: brad ages: 18 Yes I know its a for...in loop but I just can't figure out how to get this output. ...

Wordpress loop : get current post count inside The Loop

Hey, What i want to do i quite simple : when inside The Loop, i want to retrieve the current post count. For example, after every 3 posts, i want to insert an ad. So, how do i get the value of the loop count? Cheers, Kartik Rao ...

For loop in jQuery

Need to run a for loop in jquery. Condition is : i have the following code <div id="container"> <ul> <li style="background-color:#CCC">First</li> <li style="background-color:#CCC">Second</li> <li style="background-color:#CCC">Third</li> <li style="background-color:#CCC">Fourth</li> <li style="background-color:#999...

avoidind a loop in R

I will like to avoid a loop in the following code: delta_S <- function(Ro, Rr, Ir, S1, S2, S3, S4, chromaty) { .... etc .....} for (i in 1:nrow(Rrecon)) { gri[i, 6] <- delta_S(Ro=as.vector(Rrecon[i, ]), Rr=data_base$bck, Ir=data_base$Ir, S1=data_base$s1, S2=data_base$s2, S3=data_base$s3, S4=data_base$s4, chromaty="tetra") } My pr...

help with php dates and loops

Hello I currently have this PHP Script <?php if($purchase_dates != FALSE){?> <?php foreach($purchase_dates as $id=>$outer_value) { ?> <!-- Output the date here --> <!-- Start Purchase table --> <? $date =...

vb.net loop through datarow and add each value into a listbox

Private Function ColumnEqual(ByVal A As Object, ByVal B As Object) As Boolean If A Is DBNull.Value And B Is DBNull.Value Then Return True End If If A Is DBNull.Value Or B Is DBNull.Value Then Return False End If Return A = B End Function ..... Public ...

Do while loop in R

I have a Program in R where i'm using do while loop in the following format for(i in 1:n) { repeat { #code if(condition){break} } } I have a problem even if the condition in if statement is true the loop is been executed still.Can anyone help me with this Thank you ...

Comparing javascripts native "for loop" to prototype's each()

So I was having a debate with a fellow engineer about looping in JavaScript. The issue was about the native for loop construct and prototype's each() method. Now I know there are lots of docs/blogs about for and for-each, but this debate is somewhat different and I would like to hear what some of you think. Let's take the following loop...

Permutation/Algorithm to Solve Conditional Fill Puzzle

I've been digging around to see if something similar has been done previously, but have not seen anything with the mirrored conditions. To make swallowing the problem a little easier to understand, I'm going to apply it in the context of filling a baseball team roster. The given roster structure is organized as such: C, 1B, 2B, 3B, SS...

Question foreach on arrays returned by functions

i wonder if i do foreach (func_to_return_array() as $item) { ... } will it call func_to_return_array() many times (the array length)? if it does i guess it will be better to use $arr = func_to_return_array(); foreach ($arr as $item) { ... } ...

MySQL Fetch Array doubt

Hi everybody, I´m fetching my database for images to create a gallery. Every row appear inside a <li>. My question is, is it possible, that the first <li> have a class (for example, "visible"), and all the other <li> have a class named "hidden". So the first $row would have a different class than the following... Hope I made myself clear...

How to create an efficient loop for performing actions every X minutes in windows services?

I'm writing a windows service that should perform an action every, lets say, 60 seconds. How is the best way to implement that main loop? Implementations I've seen so far: 1) Using a Timer object that executes a delegate every xx seconds 2) Using ManualResetEvents (the implementation I've seen only executes once, but as far as I unders...