NSIS CharAt Macro

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

[bash]
;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
[/bash]

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

[bash]
!insertmacro CharAt "Hello World!" 1 $0
;$0 will have the letter ‘e’ inside.
[/bash]

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

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…)

[php]
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");
[/php]

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

eclipse.ini gone after Helios update? No worries

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:
[bash]#!/bin/bash
./eclipse -vmargs -XX:MaxPermSize=512M-Xms512m -Xmx1024m -XX:+UseParallelGC -XX:PermSize=256M
[/bash]

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

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

Here’s the new location:

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

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)