Archive for the ‘Other’ Category

Flash ActionScript 3.0 – TextField buttonMode

Tuesday, November 17th, 2009

Normally you use Sprite.buttonMode = true. Mousing over a TextFields brings up a potentially annoying caret. To get the buttonMode working on a TextField, place the TextField into a Sprite and:

Sprite.buttonMode = true;
Sprite.mouseChildren = false;

PHP – array_map + create_function

Sunday, September 20th, 2009

For use with php < 5.3. If you're using 5.3 just use a proper anonymous function.

array_map(create_function(‘$v’, ‘return strtolower($v).”s”;’), $a);

Python eval

Monday, September 7th, 2009

Generally, just use ‘exec’ statement instead of the ‘eval’ function

v = 1
exec 'v = 2'
print v

Python eval is a function meaning it doesn’t allow setting of variables. Usage is:
x = 1
print eval(‘x+1′)

Perl str_replace

Sunday, August 9th, 2009

$s =~ s!\Q$search!$replacement!g;

\Q will quote a string until \E (if \E is ommitted will quote the whole string). You need to quote the string because s/// uses regexp;

You can view more Perl written in PHP here.

Netbeans + CakePHP – syntax highlighting for .ctp files

Thursday, August 6th, 2009

Tools -> Options -> Miscellaneous -> Files

New File Extension (ctp)

Associate with PHP

Viola! CTP files are now treated as PHP files!

Mysql from an external file

Sunday, July 26th, 2009

From the command-line:

mysql -u username -ppassword database < filename

From within MySQL:

Log into mysql i.e. mysql -u -p [database]
mysql> source [filename]

PHP – Split with delimiter caputre

Thursday, July 23rd, 2009

Split a string on a regular expression and also capture the delimiter.  This can be useful if you have regular expressions like /(cat|dog|fish)/.

$arr = preg_split($rx, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
for ($i = 0; $i < count($arr); $i+=2) {
  $chunk = $arr[0];
  $delim = $arr[1];
}

Python – Getting dot to match newlines as well

Thursday, July 23rd, 2009

re.DOTALL / re.S – means dot will match newlines

re.MULTILINE / re.M – means that ^$ will only match start and end of string.  By default they match start and end of each line

match = re.search(r'^(.+lorem ipsum)(.+)(donor kebab.+)$', s, re.DOTALL + re.MULTILINE)

				

Perl – quick read text files scripts

Tuesday, July 21st, 2009

These two Perl utility scripts are used to quickly read a text file into either a string or an array.

sub read_file_into_string {
  my $s = '';
  open FH, @_[0];
  for (<FH>) { $s .= $_; }
  close FH;
  return $s;
}
sub read_file_into_array {
  my @lines = ();
  open FH, @_[0];
  for (<FH>) {
    chomp;
    push @lines, $_;
  }
  close FH;
  return @lines;
}

VBA – write to UTF file (with BOM)

Tuesday, July 14th, 2009

Dim objStream
Set objStream = CreateObject("ADODB.Stream")
objStream.Open
objStream.Charset = "UTF-8"
objStream.Position = 0
objStream.WriteText sTxt
objStream.SaveToFile sFileName, 2
objStream.Close