<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: array code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 14:45:24 GMT</pubDate>
    <description>DZone Snippets: array code</description>
    <item>
      <title>Array#replace_pattern</title>
      <link>http://snippets.dzone.com/posts/show/5762</link>
      <description># If you are treating Arrays like s-expressions, you might want to replace a particular pattern.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Array.class_eval do&lt;br /&gt;  def replace_pattern(pattern, value)&lt;br /&gt;    copy = self.dup&lt;br /&gt;    (0..copy.length).each do |i|&lt;br /&gt;      if copy[i] == pattern&lt;br /&gt;        copy[i] = value&lt;br /&gt;      elsif copy[i].is_a? Array&lt;br /&gt;        copy[i] = copy[i].replace_pattern(pattern, value)&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;    copy&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 13 Jul 2008 19:36:38 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5762</guid>
      <author>farleyknight (Farley Knight)</author>
    </item>
    <item>
      <title>foreach like syntax in JavaScript</title>
      <link>http://snippets.dzone.com/posts/show/5761</link>
      <description>From justsomeguy's post to &lt;a href="http://forums.invisionpower.com/lofiversion/index.php/t168975.html"&gt;http://forums.invisionpower.com/lofiversion/index.php/t168975.html&lt;/a&gt;:&lt;br /&gt;&lt;br /&gt;Since all JavaScript objects are really just associative arrays, there is a "foreach" like syntax for the "for" construct. Without it, it would be very hard to work with many common JavaScript objects. It's actually very simple to use and incredibly useful if you like associative arrays.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;//this is safe only if you can assure object has not been extended.&lt;br /&gt;var array = new Object();&lt;br /&gt;//use the below if Object has been extended&lt;br /&gt;var array;&lt;br /&gt;//everything below here works fine regardless of the two above cases&lt;br /&gt;array['something'] = 'foo';&lt;br /&gt;array['somethingelse'] = 'bar';&lt;br /&gt;for (keyVar in array) {&lt;br /&gt;   alert(keyVar + '--' + array[keyVar]);&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sat, 12 Jul 2008 23:54:19 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5761</guid>
      <author>jeffreybarke (Jeffrey Barke)</author>
    </item>
    <item>
      <title>Extending Array Prototype</title>
      <link>http://snippets.dzone.com/posts/show/5753</link>
      <description>Extending array prototype with sum, minimum, maximum and remove&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;Array.prototype.sum = function(){&lt;br /&gt;	for (var i=0, sum=0; i &lt; this.length; sum += this[i++]);&lt;br /&gt;	return sum;&lt;br /&gt;}&lt;br /&gt;Array.prototype.max = function(){&lt;br /&gt;	return Math.max.apply({},this)&lt;br /&gt;}&lt;br /&gt;Array.prototype.min = function(){&lt;br /&gt;	return Math.min.apply({},this)&lt;br /&gt;}&lt;br /&gt;Array.prototype.remove=function(s){&lt;br /&gt;	for (i=0; i &lt; this.length; i++){&lt;br /&gt;		if (s == this[i]) this.splice(i, 1);&lt;br /&gt;	}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 10 Jul 2008 08:37:08 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5753</guid>
      <author>demods ()</author>
    </item>
    <item>
      <title>Converting a numerical string into an array</title>
      <link>http://snippets.dzone.com/posts/show/5711</link>
      <description>irb(main):097:0&gt; c = "080629".scan(/\d\d/)&lt;br /&gt;=&gt; ["08", "06", "29"]&lt;br /&gt;</description>
      <pubDate>Sun, 29 Jun 2008 19:35:02 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5711</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Split array or string into smaller arrays</title>
      <link>http://snippets.dzone.com/posts/show/5641</link>
      <description>Splits an array or string into smaller arrays of the given size. Inspired on the ruby one ;).&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;import math&lt;br /&gt;# v = value to split, l = size of each chunk&lt;br /&gt;f = lambda v, l: [v[i*l:(i+1)*l] for i in range(int(math.ceil(len(v)/float(l))))]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Example&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt;&gt; f('000000111111222222333333444444567', 6)&lt;br /&gt;['000000', '111111', '222222', '333333', '444444', '567']&lt;br /&gt;&lt;br /&gt;&gt;&gt;&gt; f(tuple('000000111111222222333333444444567'), 6)&lt;br /&gt;[('0', '0', '0', '0', '0', '0'), ('1', '1', '1', '1', '1', '1'), ('2', '2', '2', '2', '2', '2'), ('3', '3', '3', '3', '3', '3'), ('4', '4', '4', '4', '4', '4'), ('5', '6', '7')]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Long live python :). Of course, a lisp/haskell implementation probably blows this snippet out.</description>
      <pubDate>Fri, 13 Jun 2008 19:37:23 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5641</guid>
      <author>santiago.aguiar (Santiago Aguiar)</author>
    </item>
    <item>
      <title>convert a 1D array wave file to a stereo 2D array</title>
      <link>http://snippets.dzone.com/posts/show/5525</link>
      <description>&lt;code&gt;&lt;br /&gt;    private int[][] zetOmNaarStereoStream(byte[] arrbBuffer){&lt;br /&gt;        int lengte = arrbBuffer.length / 4;&lt;br /&gt;        int[][] arriUitvoer = new int[2][lengte];&lt;br /&gt;        for (int iTeller=0 ; iTeller&lt;lengte ; iTeller++){&lt;br /&gt;            //Linker kanaal&lt;br /&gt;            arriUitvoer[0][iTeller] = (arrbBuffer[((iTeller * 4) + 0)] &amp; 0xFF) | (arrbBuffer[((iTeller * 4) + 1)] &lt;&lt; 8);&lt;br /&gt;            //Rechter kanaal&lt;br /&gt;            arriUitvoer[1][iTeller] = (arrbBuffer[((iTeller * 4) + 2)] &amp; 0xFF) | (arrbBuffer[((iTeller * 4) + 3)] &lt;&lt; 8);&lt;br /&gt;        }&lt;br /&gt;        return arriUitvoer;&lt;br /&gt;    }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 20 May 2008 12:30:44 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5525</guid>
      <author>tiskevin (tiskevin)</author>
    </item>
    <item>
      <title>Open an arbitrary number of resources safely in ruby</title>
      <link>http://snippets.dzone.com/posts/show/5420</link>
      <description>I'm too lazy to work out what happens if I try &lt;code&gt;filenames.map {|f| File.open(f) }&lt;/code&gt; and the thirteenth file doesnt exist, but I bet I don't like it.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;module Enumerable&lt;br /&gt;  # Example:&lt;br /&gt;  # ['a','b'].with_files {|f,g| ... }&lt;br /&gt;  # is the same as&lt;br /&gt;  # File.open('a') {|f| File.open('b') {|g| ... } }&lt;br /&gt;  # You can specify modes with&lt;br /&gt;  # [['a', 'rb'], ['b', 'w']].with_files ...&lt;br /&gt;  def with_files(&lt;br /&gt;      meth = File.method(:open),&lt;br /&gt;      offset=0,&lt;br /&gt;      inplace=false,&lt;br /&gt;      &amp;block&lt;br /&gt;  )&lt;br /&gt;    if inplace then&lt;br /&gt;      if offset &gt;= length then&lt;br /&gt;        yield self&lt;br /&gt;      else&lt;br /&gt;        fname,mode = *self[offset]&lt;br /&gt;        File.open(fname,mode) {|f| &lt;br /&gt;          self[offset] = f&lt;br /&gt;          self.with_files(meth,offset+1,true,&amp;block)&lt;br /&gt;        }&lt;br /&gt;      end&lt;br /&gt;    else&lt;br /&gt;      dup.with_files(meth,offset,true,&amp;block)&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;/code&gt;</description>
      <pubDate>Tue, 22 Apr 2008 00:44:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5420</guid>
      <author>tunah (Sam McCall)</author>
    </item>
    <item>
      <title>Array#pad!</title>
      <link>http://snippets.dzone.com/posts/show/5331</link>
      <description>My ri docco says there is an Array#pad! but runtime Ruby says there isn't. Bah. So I wrote one:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Array&lt;br /&gt;  def pad!(expected_length, pad_item = nil)&lt;br /&gt;    while expected_length &gt; length&lt;br /&gt;      self &lt;&lt; pad_item&lt;br /&gt;    end&lt;br /&gt;    self&lt;br /&gt;  end&lt;br /&gt;end&lt;/code&gt;</description>
      <pubDate>Mon, 07 Apr 2008 10:44:20 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5331</guid>
      <author>nicwilliams (Dr Nic Williams)</author>
    </item>
    <item>
      <title>Concat two array</title>
      <link>http://snippets.dzone.com/posts/show/5312</link>
      <description>var sNames:Array = ["Rajesh","Buvanesh","Latha","Ramesh"];&lt;br /&gt;var sPhones:Array = ["9940350708","00000000","044-42151184","9841026070"];&lt;br /&gt;&lt;br /&gt;for (var i=0;i&lt;sNames.length;i++){&lt;br /&gt;	trace(sNames[i]);&lt;br /&gt;	trace(sPhones[i]);&lt;br /&gt;}</description>
      <pubDate>Wed, 02 Apr 2008 03:03:45 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5312</guid>
      <author>rcube9 (rajesh)</author>
    </item>
    <item>
      <title>Array.push</title>
      <link>http://snippets.dzone.com/posts/show/5311</link>
      <description>var sEmployees:Array = [];&lt;br /&gt;sEmployees.push(["Ramesh","9841026070"]);&lt;br /&gt;sEmployees.push(["Rajesh","9940350708"]);&lt;br /&gt;sEmployees.push(["Latha","044-42151184"]);&lt;br /&gt;sEmployees.push(["Buvanesh","000-0000000"]);&lt;br /&gt;&lt;br /&gt;for (var i:Number=0; i&lt;sEmployees.length; i++){&lt;br /&gt;	trace("Name:" + sEmployees[i][0]);&lt;br /&gt;	trace("Phone:" + sEmployees[i][1]);&lt;br /&gt;}</description>
      <pubDate>Wed, 02 Apr 2008 03:03:06 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5311</guid>
      <author>rcube9 (rajesh)</author>
    </item>
  </channel>
</rss>
