loops

Clojure While Loop

I trying clojure i am trying to figure out how to implement the following algorithm, I am reading from an input stream i want to continue reading until it is not a delimiter character. i can do this in java with a while loop but i can't seem to figure out how to do it in clojure? while read readChar != delimiter do some pr...

How do I break a for loop in PHP if conditions are met?

Hi, I'm diligently plugging away at some code that checks for divisibility (yes, it's to generate primes) and I want to know how to stop a for... loop if the condition is met once. Code like this: $delete = array(); foreach ($testarray as $v) { for ($b=2; $b<$v; $b++) { if ($v%$b == 0) { $delete []= $v; } } So $testarray is int...

Clojure Variables and Looping

From googling around, I found that using while loops or using variables is discouraged. Now I implemented a very simple algorithm that will read characters from an inputstream and parse accordingly: if input is 10:abcdefghej it will parse out 10 then read next 10 bytes after the colon. The thing I am kinda lost with is how I can refact...

Clojure loop reads one extra.

When length is 4 following loop executes 5 times. Reading 5 characters from the stream. (loop [i (.read stream) result "" counter length] (let [c (char i)] (println "=>" c) (if (zero? counter) result (recur (.read stream) (str result c) (dec counter))))) ...

Creating a Brainf**k parser, whats the best method of parsing loop operators?

I'm creating a Brainf++k parser (in a BASIC dialect) ultimately to create an interpreter but i've realise it's not as straight forward as i first thought. My problem is that i need a way to accurately parse the matching loop operators within a Brainf++k program. This is an example program: ,>,>++++++++[<------<------>>-] <<[>[>+>+<<-]>>...

ColdFusion loop through struct with key evaluation fails! What am i missing?

I have this code in my cfm, which works <cfif not StructIsEmpty(form)> <cfset larray = user.getArray() /> <cfloop collection="#form#" item="key"> <cfif left(key,4) eq "UPD_"> <cfset x = listLast(key,"_") /> <cfset y = evaluate(0,key) /> <cfloop index="j" from="1" to="#arrayLen(larray)#"> <cfif (larray[j][1] eq x) and (l...

How can I compare different elements of array in Perl?

I am new to this field. So kindly go easy on me. I have two arrays: @array1 = ("ABC321", "CDB672", "PLE89",....); @array2 = ("PLE89", "ABC678", "LMD789",...); I want to compare elements of these two different arrays. But, I want to only match letters with letters. So for instance, if arrays are compared, $array[2] element (PLE) shoul...

Reading a text file line by line in a batch or VBS script?

I have a text file that contains a list of filenames, minus the extension, that I need to create. I want to run a quick batch file command or VBS script that will iterate through the list and then create a file based on the name. The text file looks something like this: PRXI0000466 PRXI0000564 PRXI0000636 PRXI0000681 PRXI0001092 So ...

Identifying last loop when using for each

I want to do something different with the last loop iteration when performing 'foreach' on an object. I'm using Ruby but the same goes for C#, Java etc. list = ['A','B','C'] list.each{|i| puts "Looping: "+i # if not last loop iteration puts "Last one: "+i # if last loop iteration } The output desired is equivalent to: ...

Simplifying Data with a for loop (Python)

Hi, I was trying to simplify the code: header = [] header.append(header1) header.append(header2) header.append(header3) header.append(header4) header.append(header5) header.append(header6) where: header1 = str(input.head...

Trouble stopping a loop

I'm having trouble with memory in a j2me application. (see another question) I discovered that one class has a loop that doesn't stop until the application is closed. This loop is consuming all the memory available. I didn't make this class so I don't know why things was done this way. So any suggestions are welcome. Here is a simplif...

How do you Make A Repeat-Until Loop in C++?

How do you Make A Repeat-Until Loop in C++? As opposed to a standard While or For loop. I need to check the condition at the end of each iteration, rather than at the beginning. ...

Javascript for/in loops through properties, not indexes and returns strings.

Ok, I have this code: var room = [ { time: 0, people: 0 } ]; and then: time = 5; for( var i in room ) { if( room[i].time < time ){ spliceIndex = i + 1; } } console.log(spliceIndex); And the console reads: 01 - Which means the 1 is concatenated which further means that i is a string, and not an integer as expected. Casting t...

while (cin >> x) and end-of-file issues

Hey folks. I'm a little confused as to what's going on, i'm playing with some programs from "Accelerated C++", and have hit a problem with one of the early programs (page 35, if you happen to have a copy nearby). It uses this snippet: while (cin >> x) { ++count; sum += x; } ("count" is an integer, "x" is a double) It works as...

Any implementable code for Loop iteration dependency checking

Are there any readily available implementations (or some open source code) of loop iteration dependency verification algorithms like I-test, Banerjee test etc. ...

How to update the max loop length if the length gets smaller in the loop body?

I have the following array. <cfset ItemHasUsers = arrayNew(1)> <cfloop query="qReadData"> <cfset ItemHasUsers[qReadData.currentrow]["ID"] = qReadData.ID > <cfset ItemHasUsers[qReadData.currentrow]["Asset"] = qReadData.COUNTOFITEMS > </cfloop> I get some records from my database which i put into a table and which manipulate through a...

Outer Cross Join?

I'm trying to avoid using queries in while loops. Therefore I've come to the conclusion that I should use a cross join. Take this example: SELECT * FROM products CROSS JOIN images USING (imgId) CROSS JOIN productcolors ON colorsId = colorId WHERE productId = 1 should return two rows (table structure below): imgId | productId | colo...

Performance difference in for loop condition?

Hello all, I have a simple question that I am posing mostly for my curiousity. What are the differences between these two lines of code? (in C++) for(int i = 0; i < N, N > 0; i++) for(int i = 0; i < N && N > 0; i++) The selection of the conditions is completely arbitrary, I'm just interested in the differences between , and &&. I'm...

Turning off redirect loop error in apache

I'm crawling a website using php. As I don't know much about turning php into a CGI and runing from command line the method I've chosen is, after one succesful iteration, to redirect back to the same php file so it runs again (it's not an infinite loop as I use both a cookie and a timestamp check to make sure it ends within a set time or...

Javascript: hiding prototype methods in for loop ?

So lets say I've added some prototype methods to the Array class: Array.prototype.containsKey = function(obj) { for(var key in this) if (key == obj) return true; return false; } Array.prototype.containsValue = function(obj) { for(var key in this) if (this[key] == obj) return true; return false; } t...