<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: loop code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 03:19:32 GMT</pubDate>
    <description>DZone Snippets: loop code</description>
    <item>
      <title>A simple loop in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/4914</link>
      <description>This loop repeats 10 times, with each iteration it increments the variable 'i' by 1 and displays it's value.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;i = 0&lt;br /&gt;10.times do&lt;br /&gt;  i += 1&lt;br /&gt;  puts i&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 22 Dec 2007 19:15:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4914</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Treating first/last loop iterations differently</title>
      <link>http://snippets.dzone.com/posts/show/4353</link>
      <description>Sometimes, you need to iterate over a list of items and treat the first and last element differently from the rest. This tends to produce really messy code, which can be avoided using the following snippet.&lt;br /&gt;&lt;br /&gt;Imagine you have a list of Kittens, and you want to print out a summary of them, saying "My kittens are called Bob, James, John, and Ally." You need to treat the first and last element of the list differently from the rest, lest you end up with superfluous commas in the text. You also need to deal with the cases of there being only one kitten, or no kittens at all.&lt;br /&gt;&lt;br /&gt;To do this, you can avail yourself of a Counter class that keeps track of where you are in the iteration:&lt;br /&gt;&lt;code&gt;&lt;br /&gt;String kittenDesc = "I have no kittens.";&lt;br /&gt;Counter kc = new Counter(kittens);&lt;br /&gt;for (Kitten k : kittens) switch(kc.next()) {&lt;br /&gt;	case one:   kittenDesc =  "My kitten is called " + k.getName() + "."; break;&lt;br /&gt;	case first: kittenDesc =  "My kittens are called " + k.getName();     break;&lt;br /&gt;	case item:  kittenDesc += ", " + k.getName();                         break;&lt;br /&gt;	case last:  kittenDesc += " and " + k.getName() + ".";                break;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;package com.zarkonnen.util;&lt;br /&gt;&lt;br /&gt;import java.util.Collection;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * Used for keeping track of where you are in a collection/array being iterated over. Use by&lt;br /&gt; * initialising with the collection/array before the loop and embedding a switch on the next() Mode&lt;br /&gt; * value into the loop.&lt;br /&gt; *&lt;br /&gt; * LICENCE: This code is licenced under a BSD licence. Feel free to alter and redistribute.&lt;br /&gt; *&lt;br /&gt; * @author David Stark, http://www.zarkonnen.com&lt;br /&gt; * @version 1.0 (2007-07-11)&lt;br /&gt; */&lt;br /&gt;public class Counter {&lt;br /&gt;	/**&lt;br /&gt;	 * An enumeration of modes identifying where in the collection/array we are.&lt;br /&gt;	 */&lt;br /&gt;	public enum Where {&lt;br /&gt;		/** The only element of an array/collection of size 1. */ one,&lt;br /&gt;		/** The first element. */                                 first,&lt;br /&gt;		/** The last element of the array/collection. */          last,&lt;br /&gt;		/** Any other element somewhere in the middle. */         item&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	private int size;&lt;br /&gt;	private int nextIndex = 0;&lt;br /&gt;	&lt;br /&gt;	/**&lt;br /&gt;	 * @param c A collection to keep track of. If its size changes between now and the&lt;br /&gt;	 * iteration, strange things will happen.&lt;br /&gt;	 */&lt;br /&gt;	public Counter(Collection c) {&lt;br /&gt;		size = c.size();&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	/**&lt;br /&gt;	 * @param a An array to keep track of. If its size changes between now and the iteration,&lt;br /&gt;	 * strange things will happen.&lt;br /&gt;	 */&lt;br /&gt;	public Counter(Object[] a) {&lt;br /&gt;		size = a.length;&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	/**&lt;br /&gt;	 * @return A Where enum value for where in the array/collection we now are:&lt;br /&gt;	 * &lt;ul&gt;&lt;br /&gt;	 * &lt;li&gt;&lt;strong&gt;one&lt;/strong&gt; at the only element of an array/collection of size 1&lt;/li&gt;&lt;br /&gt;	 * &lt;li&gt;&lt;strong&gt;first&lt;/strong&gt; at the first element&lt;/li&gt;&lt;br /&gt;	 * &lt;li&gt;&lt;strong&gt;last&lt;/strong&gt; at the last element&lt;/li&gt;&lt;br /&gt;	 * &lt;li&gt;&lt;strong&gt;item&lt;/strong&gt; at any other element&lt;/li&gt;&lt;br /&gt;	 * &lt;/ul&gt;&lt;br /&gt;	 */&lt;br /&gt;	public Where next() {&lt;br /&gt;		return size == 1         ? Where.one   :&lt;br /&gt;		       nextIndex++ == 0  ? Where.first :&lt;br /&gt;		       nextIndex == size ? Where.last  :&lt;br /&gt;		                           Where.item  ;&lt;br /&gt;	}&lt;br /&gt;	&lt;br /&gt;	/**&lt;br /&gt;	 * @return Which index of the array/collection we're currently at.&lt;br /&gt;	 */&lt;br /&gt;	public int index() {&lt;br /&gt;		return nextIndex == 0 ? 0 : nextIndex - 1;&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 25 Jul 2007 10:10:01 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4353</guid>
      <author>Zarkonnen (David Stark)</author>
    </item>
    <item>
      <title>Using cycling through input variables in PHP using foreach</title>
      <link>http://snippets.dzone.com/posts/show/3699</link>
      <description>This is a simple mechanism for cycling through all the inputs to a PHP script. If you're using $_GET (variables in the query string), $_POST (variables posted with a POST method on a form) or even $_SESSION (variables stored in the session) you can use this technique.&lt;br /&gt;&lt;br /&gt;This snippet prints out all the variables in the $_GET variable (the query string) .&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;foreach ($_GET as $key =&gt; $value)&lt;br /&gt;{&lt;br /&gt;print "$key has a value of $value";&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 19 Mar 2007 16:02:20 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3699</guid>
      <author>brainwipe (Rob Lang)</author>
    </item>
    <item>
      <title>ForLoop: a simple for loop in xslt</title>
      <link>http://snippets.dzone.com/posts/show/930</link>
      <description>&lt;code&gt;&lt;br /&gt;&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;&lt;br /&gt;&lt;!--&lt;br /&gt;### begin_: file metadata&lt;br /&gt;    ### &lt;region-file_info&gt;&lt;br /&gt;    ### main:&lt;br /&gt;    ###   - name : ForLoop: a simple for loop in xslt&lt;br /&gt;    ###     desc : |&lt;br /&gt;    ###         Do a simple for loop in xslt displaying hello world.&lt;br /&gt;    ###         Call this from any source xml file.&lt;br /&gt;    ###         It works independently of the data in the xml.&lt;br /&gt;    ###     date : created="Thu 2005-12-01 11:30:52"&lt;br /&gt;    ###     last : lastmod="Thu 2005-12-01 11:30:57"&lt;br /&gt;    ###     lang    : xslt&lt;br /&gt;    ###     tags    : xml xslt loop for hello_world&lt;br /&gt;    ### &lt;/region-file_info&gt;&lt;br /&gt;    --&gt;&lt;br /&gt;&lt;xsl:stylesheet version="1.0"&lt;br /&gt;    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;&lt;br /&gt;    &lt;xsl:output method="html"/&gt;&lt;br /&gt;&lt;br /&gt;&lt;xsl:template match="/"&gt;&lt;br /&gt;&lt;br /&gt;&lt;html&gt;&lt;br /&gt;&lt;head&gt;&lt;title&gt;Say Hello Ten Times!&lt;/title&gt;&lt;/head&gt;&lt;br /&gt;&lt;body&gt;&lt;br /&gt;    &lt;b&gt;I am going to say hello Ten Times!&lt;/b&gt;&lt;br /&gt;&lt;!-- begin_: Send_Loop_To_HTML --&gt;&lt;br /&gt;    &lt;xsl:call-template name="for.loop"&gt;&lt;br /&gt;     &lt;xsl:with-param name="i"&gt;1&lt;/xsl:with-param&gt;&lt;br /&gt;     &lt;xsl:with-param name="count"&gt;10&lt;/xsl:with-param&gt;&lt;br /&gt;    &lt;/xsl:call-template&gt;&lt;br /&gt;&lt;/body&gt;&lt;br /&gt;&lt;/html&gt;&lt;br /&gt;&lt;br /&gt;&lt;/xsl:template&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;!--begin_: Define_The_Output_Loop --&gt;&lt;br /&gt;  &lt;xsl:template name="for.loop"&gt;&lt;br /&gt;&lt;br /&gt;   &lt;xsl:param name="i"      /&gt;&lt;br /&gt;   &lt;xsl:param name="count"  /&gt;&lt;br /&gt;&lt;br /&gt;   &lt;!--begin_: Line_by_Line_Output --&gt;&lt;br /&gt;   &lt;xsl:if test="$i &amp;lt;= $count"&gt;&lt;br /&gt;      &lt;br /&gt; &lt;b&gt;&lt;xsl:value-of select="$i" /&gt;.&lt;/b&gt;Hello world!&lt;br /&gt;   &lt;/xsl:if&gt;&lt;br /&gt;&lt;br /&gt;   &lt;!--begin_: RepeatTheLoopUntilFinished--&gt;&lt;br /&gt;   &lt;xsl:if test="$i &amp;lt;= $count"&gt;&lt;br /&gt;      &lt;xsl:call-template name="for.loop"&gt;&lt;br /&gt;          &lt;xsl:with-param name="i"&gt;&lt;br /&gt;              &lt;xsl:value-of select="$i + 1"/&gt;&lt;br /&gt;          &lt;/xsl:with-param&gt;&lt;br /&gt;          &lt;xsl:with-param name="count"&gt;&lt;br /&gt;              &lt;xsl:value-of select="$count"/&gt;&lt;br /&gt;          &lt;/xsl:with-param&gt;&lt;br /&gt;      &lt;/xsl:call-template&gt;&lt;br /&gt;   &lt;/xsl:if&gt;&lt;br /&gt;&lt;br /&gt;  &lt;/xsl:template&gt;&lt;br /&gt;&lt;/xsl:stylesheet&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 02 Dec 2005 06:33:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/930</guid>
      <author>drefty (drefty)</author>
    </item>
    <item>
      <title>Using ao_sleep</title>
      <link>http://snippets.dzone.com/posts/show/738</link>
      <description>In python for series 60, the use of e32.ao_sleep is encourage&lt;br /&gt;over time.sleep. AO stands for 'Active Object' approach to&lt;br /&gt;cooperative multi-tasking. When an object go to 'ao_sleep'&lt;br /&gt;other active objects can run.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import e32&lt;br /&gt;e32.ao_sleep(5)  # sleep for 5 seconds&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;There is another usage ao_sleep(interval, callback)&lt;br /&gt;where ao_sleep will return immediately but the callback&lt;br /&gt;will be called after the interval (in another thread?)&lt;br /&gt;&lt;br /&gt;This could be used to create a repeat loop for every interval.&lt;br /&gt;I show this in &lt;a href=http://bigbold.com/snippets/posts/show/730&gt;a previous snippet&lt;/a&gt;.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import e32, time&lt;br /&gt;&lt;br /&gt;def showtime():&lt;br /&gt;  print time.clock()&lt;br /&gt;  e32.ao_sleep(1, showtime)  # sleep then call itself again&lt;br /&gt;&lt;br /&gt;showtime()  # start the loop&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 18 Sep 2005 13:59:03 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/738</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
