iteration

Rails - iterate over db entries in one field, separated by linebreaks

Hi everyone, In my application I have a products model which has among other things four fields for image paths. I use this to build a slide show. However, I would love to have all those paths in one big text field and seperate them by whatever works (linebreak would be the easiest to handle in the form). I was thinking something li...

Python's CSV reader and iteration

I have a CSV file that looks like this: "Company, Inc.",,,,,,,,,,,,10/30/09 A/R Summary Aged Analysis Report,,,,,,,,,,,,10:35:01 All Clients,,,,,,,,,,,,USER Client Account,Customer Name,15-Jan,16 - 30,31 - 60,61 - 90,91 - 120,120 - Over,Total,Status,Credit Limit 1000001111,CLIENT A,0,0,"3,711.32",0,0,"18,629.64","22,340.96",COD,"20,000...

Is there a way to fill a collection using a LINQ expression?

One of the great things about LINQ is that allows you to get information that's related to a collection without having to manually write code to iterate through the collection. Is there a way to use LINQ to populate a collection, thus avoiding the need to write a loop? For example, let's say I have the following code which works with...

Opening different windows in wxPython using strings from a list

I have a list of file locations stored as strings. I want to be able to open a separate window for all of the different strings. What would be the best way to do that? Essentially, You click a button, the strings are constructed and they are left in a list. When I was prototyping, I built a small program to display the contents of one st...

Utilizing objects in another class in Python

In code(pseudo) like this def path(): dirList = ['c:\\', 'y:\\', 'z:\\'] home_folder = 'peter.txt' complete = [s + home_folder for s in dirList] print complete def fileWrite(): filename = 'c:\peter.txt' text = 'Hello World' file = open(filename, 'w') file.write(text) file.close() I can make both wo...

Is there a reason that we cannot iterate on "reverse Range" in ruby?

I tried to iterate backwards with ruby using a Range and each. This way: (4..0).each do |i| puts i end ==> 4..0 Iteration through 0..4 writes the numbers. On the other Range r = 4..0 seems to be ok, r.first == 4, r.last == 0. Seems to be strange to me that the construct above does not produce the expected result. What is the a reaso...

Can all iterative algorithms be expressed recursively?

If not, is there a good counter example that shows an iterative algorithm for which there exists no recursive counterpart? If it is the case that all iterative algorithms can be expressed recursively, are there cases in which this is more difficult to do? Also, what role does the programming language play in all this? I can imagine tha...

Iterate over a list, returning the current, next and the element before current

Hello StackOverflow, I have problems with writing an specific application in a scala-esque and elegant way. I tried this for some time now, but I cannot find a "good" solution to this Problem: Given that I have the following List: List("foo", "bar", "baz", "blah") I want to Iterate over this list, not only giving me the current elem...

Does c#/.net x.x have an implementation of a doubly linked list (that can be iterated over backwards)?

I've been searching for the standard implementation of a doubly linked list in c# (so that I have a linked list I can iterate over backwards) and cannot find one. I feel like something so simple must have an implementation that I'm just missing. If it does exist, for which version of c#/.net does it exist? Reverse iteration in general ...

Removal of elements during iteration through a list - safety

I was wondering if something like this is safe... // Iterating through a <list> while ( iter != seq.end()) { if ( test ) { iter = seq.erase( iter ); } else { ++iter; } I know that iterating through a vector in this way would invalidate the iterator, but would the same thing occur in a list? I assume not since a list is se...

Getting next element while cycling through a list

li = [0, 1, 2, 3] running = True while running: for elem in li: thiselem = elem nextelem = li[li.index(elem)+1] When this reaches the last element, an IndexError is raised (as is the case for any list, tuple, dictionary, or string that is iterated). I actually want at that point for nextelem to equal li[0]. My rath...

Python Iteration Problem!!

I have this code here, it is supposed to remove the common letters from both the lists n1 and n2. But when i run this code it only runs once as in it removes only 'a' from both n1 and n2 and doesnt remove 'k'. Just to clarify this code should always work on only 2 words. name1 = "abdjek" name2 = "doarhsnk" n1l = list(name1) n2l = list...

Iterate through CheckedListBox in WinForms?

Hi everyone, If I have Checked list box in Win forms which I fill like this List<Tasks> tasks = db.GetAllTasks(); foreach (var t in tasks) tasksCheckedListBox.Items.Add(t.Name); How can I iterate tasksCheckedListBox.Items and set some check boxes as checked? Thanks ...

jquery each function for dynamically elements

I am dynamically adding a class to my code using Jquery's .attr('class', 'show'). But when I use .each() function like this: $('.show a').each(function() { alert(0); }); it doesen't work. I works when I added the 'show' class manually. How can I do this dynamically? ...

Why should recursion be preferred over iteration ?

Iteration is more performant than recursion, right? Then why do some people opine that recursion is better (more elegant, in their words) than iteration? I really don't see why some languages like Haskell do not allow iteration and encourage recursion? Isn't that absurd to encourage something that has bad performance (and that too when m...

jQuery recursive iteration over objects

The other day I thought I saw an object iterator in jQuery that had a flag that could be set to recursively iterate over child objects. I thought it was part of jQuery.each(), but now I don't see that capability in the docs. Is there any such iterator in jQuery that can be automatically recursive? (I know how to do it in javascript. J...

Multidimensional array iteration

Say you have the following array: $nodes = array( "parent node", "parent node", array( "child node", "child node", array( "grand child node", "grand child node"))); How would you go about transforming it to an XML string so that it looks like: <node> <node>parent node</n...

Is there a way to iterate and reflect the member names and values contained in enumerations?

Let's say I have the following enum: public enum Colors { White = 10, Black = 20, Red = 30, Blue = 40 } I'm wondering if there is a way to iterate through all the members of Colors to find the member names and their values. ...

Printing to a file from a list of lists in Python

I am trying to print to a file that will look like: 'A' '1' 'B' '2' 'C' '3' Given the code below, however, the result is : ['A'] ['B'] ['C'] This is probably a 'softball' question, but what am I doing wrong here? l1 = ['1'] l2 = ['A'] l3 = ['2'] l4 = ['B'] l5 = ['3'] l6 = ['C'] listoflists = [l1,l2,l3,l4,l5,l6] itr = iter(listof...

How do I iterate "between" items in an array / collection / list?

This problem has bugged me for years, and I always feel like I'm coming up with a hack when there's a much better solution. The issue at hand occurs when you want to do something to all items in a list and then add something inbetween those items. In short, I want to: Do something to every item in the list. Do something else to all b...