Translate

Archive for March, 2011

NSIS CharAt Macro

Monday, March 21st, 2011

Lord knows why the NSIS folks didn’t add a CharAt function to their API.
In case you need it to parse a string, here’s a Macro that makes StrCpy work like a CharAt function

;Get a character inside a string given an index.
;Usage: CharAt String Index Output
!macro CharAt InputString Index Output
 StrCpy ${Output} ${InputString} 1 ${Index}
!macroend

To use the CharAt “function” (macro in this case), do as follows:

!insertmacro CharAt "Hello World!" 1 $0
;$0 will have the letter 'e' inside.
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

Get a randomly weighted key in a Map/Dictionary/Associative Array

Tuesday, March 15th, 2011

Here’s a very useful function to retrieve a randomly weighted key from an associative array in PHP.

Sometimes you need to fetch random elements from a collection but you need some elements to have a little more chance than others to be fetched (business rules or whatever…)

srand(time()); //dont call this inside the function or you'll get same result always
function getRandomWeightedKey($weighted_elements, $default_key) {
  $weighted_ranges = array();
  $weight_offset = 0;

  //if you know weights are not gonna change you could
  //move this part of the code to another place and cache it
  //for better performance. Specially if you have a lot of weighted elements
  foreach ($weighted_elements as $key => $weight) {
    $weighted_ranges[]=array('key'=>$key,
                             'range'=>array(intval($weight_offset),intval($weight_offset+$weight)));
    $weight_offset += $weight;
  }

  //get a random number between the 0 and the maximum
  $r = rand(0,$weight_offset);

  foreach ($weighted_ranges as $range) {
    if ($r >= $range['range'][0] && $r < $range['range'][1]) {
      return $range['key'];
    }
  }

  //this shouldn't happen but Mr. Justin Case.
  return $default_key;
}

//how to use it. (weights don't necessarily need to add to 100,
//but it helps me to visualize things basing weights on 100
$weights = array("a" => 25, "b" => 25, "c" => 50);

$randomKey = getRandomWeightedKey($weights, "c");

It should be trivial porting this code to the language of your choice. Enjoy.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

eclipse.ini gone after Helios update? No worries

Saturday, March 5th, 2011

So you had a pimped out eclipse.ini file and for some odd reason in the world, eclipse.ini is missing in action after one of the numerous updates, you try to put it where it was, but none of the options you pass it seem to have an effect.

Here’s what I did

After I gave up on reading the documentation on what the hell happened to eclipse.ini and why it’s not having any effect on my eclipse, I just created my own eclipse launcher.

Here’s what it has inside:

#!/bin/bash
./eclipse -vmargs -XX:MaxPermSize=512M-Xms512m -Xmx1024m -XX:+UseParallelGC -XX:PermSize=256M
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

New Location of src.jar for JDK 1.6.0_24 on Mac OS X

Saturday, March 5th, 2011

These Apple devs… why do you keep moving src.jar?

Here’s the new location:

/Library/Java/JavaVirtualMachines/1.6.0_24-b07-330.jdk/Contents/Home/src.jar

Not there!
If you don’t see src.jar there, and you’re running Mac OSX 10.6, and you did download the Java system update, you will have to go to developer.apple.com and download the “Java for Mac OS X 10.6 (10M3321) (Disk Image)” (or whatever is current and makes sense for your Mac OSX version)

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)


  • Categories

  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • July 2010
  • June 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • October 2009
  • September 2009
  • July 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004