<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Blah Blah Woof Woof comments on Evenly dividing array elements in bash</title>
    <link>http://vault.openmonkey.com/</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Blah Blah Woof Woof comments</description>
    <item>
      <title>"Evenly dividing array elements in bash": comment by Vidar Bashman Madsen</title>
      <description>Lacking a preview option and no info on tags, I hope this is formatted properly...

How about this approach; Iterate through the items, if item_number % days = today, process item.

The code:

&lt;pre&gt;&lt;code&gt;
 i=0
 for item in ${items[@]}; do
   if [ $[$i % $days] = $today ]; then
     echo "Process $item..."
   fi
   i=$[$i+1]
 done
&lt;/code&gt;&lt;/pre&gt;
</description>
      <pubDate>Thu, 29 Mar 2007 06:54:26 PDT</pubDate>
      <guid>http://vault.openmonkey.com/articles/2007/03/27/evenly-dividing-array-elements-in-bash#comment-161</guid>
      <link>http://vault.openmonkey.com/articles/2007/03/27/evenly-dividing-array-elements-in-bash#comment-161</link>
    </item>
    <item>
      <title>"Evenly dividing array elements in bash" by triley</title>
      <description>&lt;p&gt;Writing in bash is fun because of the constraints it offers, but I&amp;#8217;m never sure if I am choosing the most straightforward way to do things.&lt;/p&gt;


	&lt;p&gt;A few weeks ago I had to write a script to vacuum a bunch of databases once per week.  Instead of doing them all at once, I wanted to evenly spread the load across the seven days.&lt;/p&gt;


	&lt;p&gt;I wrote something that works pretty well, I think, but it does seem a little hackish, and I&amp;#8217;d love to hear if there is a better way to achieve this.&lt;/p&gt;


	&lt;p&gt;Here&amp;#8217;s what I did, check it out:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt; # 11 elements in this example
 # Should be processed 2 per day for the first 4 days, then 1 per day for the reamining 3 days
 items=("mario" "luigi" "toad" "peach" "yoshi" "wario" "koopa" "bowser" "lakitu" "birdo" "fryguy")

 days=7

 # number of the current day, starting from 0
 let "today = `date +%u` - 1" 

 # number of whole that can be processed per day
 let "per_day = ${#items[*]} / ${days}" 

 # remainder of items that must also be processed
 let "remainder = ${#items[*]} % ${days}" 

 # index of array element to start iterating from for the current day
 if [ $today -gt $remainder ]; then
   let "start = (${per_day} * ${today}) + ${remainder}" 
 else
   let "start = (${per_day} * ${today}) + ${today}" 
 fi

 # number of elements to iterate over for the current day
 if [ $today -ge $remainder ]; then
   count=$per_day
 else
   let "count = ${per_day} + 1" 
 fi

 # Do it!
 echo "Processing ${count} items." 

 for ((i=$start; i &amp;lt; ($start+$count); i++)); do
   do_something_with_item ${items[$i]}
 done&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;If you can offer any tips or suggestions, please comment on this entry!&lt;/p&gt;

</description>
      <pubDate>Tue, 27 Mar 2007 13:08:00 PDT</pubDate>
      <guid>&lt;a href="/articles/2007/03/27/evenly-dividing-array-elements-in-bash"&gt;Evenly dividing array elements in bash&lt;/a&gt;</guid>
      <link>&lt;a href="/articles/2007/03/27/evenly-dividing-array-elements-in-bash"&gt;Evenly dividing array elements in bash&lt;/a&gt;</link>
    </item>
  </channel>
</rss>

