RSS
 

Actionscript 3 using string or integer to access variables

18 Aug

Here’s a situation I often run into: Having a set of variables that are almost alike, but differ in number. For example:

1
2
var textbox1:TextField;
var textbox2:TextField;

Now say I have a function that needs to change these based on a number in another variable. I could do a switch to access them, but that would be repeating myself. Instead I can just access them through my string or integer variable like this:

1
2
var num:int = 1;
trace(this["textbox" + num])

You could, for instance use it to loop over your variables:

1
2
3
4
5
function update_textfield() {
   for each(var i:int = 5; i < something; i++) {
      trace(this["textbox" + i])
   }
}

And presto! Easily accessable variables :)

 

Drupal 6 jquery document ready troubleshooting

17 Aug

So jquery’s  $(document).ready() isn’t working for you in Drupal 6. Dont worry. It’s not an error on your part, and very easy to troubleshoot.

The reason for this behaviour is that $(document).ready() doesn’t exist in drupal. The reason for this is very simple. There can be only one document.ready function, and Drupal is already using it. Instead you must use Drupal’s built in hook to document.ready. So instead of this javascript:

1
2
3
$(document).ready(function() {
// put all your jQuery goodness in here.
});

Use this:

1
2
3
Drupal.behaviors.myModuleBehavior = function (context) {
// put all your jQuery goodness in here.
}

myModuleBehavior just needs to be a unique name, and is right off the top of my head.

Of course this doesn’t accept any variables (context is a fixed Drupal variable and cannot be changed), so add variables to it following this pattern via PHP code:

1
drupal_add_js(array('myvariable' => 'test'), 'setting');

and access them as such in your Javascript:

1
2
3
Drupal.behaviors.myModuleBehavior = function (context) {
 alert(Drupal.settings.myvariable);
}

Enjoy!

 
No Comments

Posted in Code, Drupal

 

Drupal 6 and jQuery UI complete Guide

17 Aug

I hope someone will benefit from this guide, and save a lot more time than I did :)

Ok, so once I had to use the fantastic jQuery UI plugin for jQuery in a Drupal 6 project, I had a hell of a time getting it to work. Eventually I did it with the help of information from three different web pages. This guide will tell you everything you need to do in order to get jQuery UI working in Drupal:

  1. Install the jQuery Update Drupal module (You need jQuery version 1.3+ for jQuery UI, which doesn’t come standard in Drupal 6)
  2. Install the jQuery UI Drupal module
  3. Create a folder named ‘jquery.ui’ in the ‘jQuery UI’ module folder ([your_drupal_directory]/sites/all/modules/jquery_ui/)
  4. Download jQuery UI 1.7.3 (NOTE: Not the latest version!) from the jQuery UI home page via this link or, if that doesnt work, my mirror of it
  5. Unzip the jQuery UI you just downloaded
  6. Upload/Copy everything from ‘jquery-ui-1.7.3.custom/development-bundle’ to ‘/sites/all/modules/jquery_ui/jquery.ui’
    Your ‘jquery.ui’ folder sohuld now contain the following files/folders:
    AUTHORS.txt
    demos
    docs
    external
    GPL-LICENSE.txt
    jquery-1.3.2.js
    MIT-LICENSE.txt
    themes
    ui
    version.txt
  7. Upload/Copy ‘jquery-ui-1.7.3.custom/development-bundle/ui’ to ‘jquery.ui/ui’ (So copy/upload the entire ui folder)
    Your ui fodler should now contain two folder and a whole lot of *.js files
  8. Upload/Copy ‘jquery-ui-1.7.3.custom/css’ to ‘jquery.ui/css’ (So copy/upload the entire css folder)
  9. Now, go to your theme folder (/sites/all/themes/[your_theme_name]) (the themes can also reside in your site directory, but ‘all’ is more common)
    In your theme folder, locate the file ‘template.php’. In this file, add these three lines anywhere outside any of the functions (I added them right after the comments in the top of the file):
    drupal_add_js('sites/all/modules/jquery_ui/jquery.ui/js/jquery-ui-1.7.3.custom.min.js');
    drupal_add_js('sites/all/modules/jquery_ui/jquery.ui/ui/ui.core.js');
    drupal_add_css('sites/all/modules/jquery_ui/jquery.ui/themes/base/ui.all.css');
  10. Now, your jQuery UI is ready to play nice with Drupal! (NOTE: A word of warning: Remember you have the 1.7.3 version of jquery UI, not the latest, so find the documentation for your version in your downloaded development-bundle/docs folder.
  11. Test your new jquery ui out:
    - Add this line to your page.tpl.php file’s <body> section:
    <div type=”text” id=”datepicker”></div>
    - And add this line to your drupal-specific document.ready function:
    $(“#datepicker”).datepicker();
  12. You should now see a datepicker nice and cute on your drupal front page.
 
7 Comments

Posted in Code

 

My LAIR IEEE published article

17 Aug

Okay – this will be the last repost of stuff from my old blog:

I had an article “LAIR: Language of Automatically Inferred Redaction” published in the IEEE paper ICSC, and as a result I, together with my project partner Steffen Hedegaard were invited (and went) to speak at the conference in Berkely, San Fransisco. Fun times. Here’s the article.

And the brag link: LAIR at IEEE

 
 

Actionscript 3 poker hand evaluator

17 Aug

This Actionscript Flash poker (5-card & Texas Hold’em) evaluator will evaluate more than 14,000 hands per second, with a size of around 6 kb. It also includes classes to wrap everything up nice and tight for convenience.

Starting from this post about poker hand evaluators, i converted the improved version of Cactukev’s poker hand evaluator to AS3 some time ago, and wrapped it in some nice OO classes to make it easily accessible.

The OO classes include ways to handle a Deck of cards, a Dealer to draw cards from it, objects to represent a 5- and 7-card hand and compare strengths, and a Card class to make card strength and textual representation easy.

The straight dope is that I have tested it to evaluate approx. 2.6 mio. 5-card poker hands in 6.3 seconds, and 133 mio. 7-card poker hands in approx. 2.5 hours, or about 14.000 hands per second. The test evaluated the correct evaluation of all possible 5- and 7-card poker hands.
The tests were run on a 2.8 gHz Macbook Pro with 4Gb RAM

The .swc binary can be found here

API reference here

The test to evaluate that it runs correctly here (AIR application)

And the source code here

License:
The original code i ported it from was released to the public domain, and is as such free to use, modify and redistribute.
I am releasing this code under the MIT license. License info is found in the source files.

 
No Comments

Posted in Code

 

Java code to get URL from a string

15 Aug

This little code snippet / function will effectively extract URL strings from a string in Java. I found the basic regex for doing it here, and used it in a java function.

I expanded on the basic regex a bit with the part “|www[.]” in order to catch links not starting with “http://”

Enough talk (it is cheap), here’s the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Pull all links from the body for easy retrieval
private ArrayList pullLinks(String text) {
ArrayList links = new ArrayList();
 
String regex = "\\(?\\b(http://|www[.])[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(text);
while(m.find()) {
String urlStr = m.group();
if (urlStr.startsWith("(") && urlStr.endsWith(")"))
{
urlStr = urlStr.substring(1, urlStr.length() - 1);
}
links.add(urlStr);
}
return links;
}
 
No Comments

Posted in Code

 

Blog hacked

15 Aug

So my old wp blog was hacked, and hence I’ve purged the whole thing and done a clean install, since I had to move servers anyway – and here it is!

I’ll also be moving the blog in a more professional direction – more on Computer Science stuff and my projects, and less “softie stuff” :-)

 
1 Comment

Posted in Ramblings