Perl convert RGB to hex HTML color

Converting RGB to hex just involves understanding the decimal representation of RGB. See below for a small perl procedure.

sub RGB2Hex
{
my $r = shift;
my $g = shift;
my $b = shift;
my $dec = 0;

$dec += 256*256*$r;
$dec += 256*$g;
$dec += $b;

return sprintf(“%.6x”, $dec);
}

Get source code from GitHub with git (read-only)

If you want to get source code from GitHub it’s easy enough to click ‘Download’ if you just want an archive.  However if you’re on GitHub you might want to use the proper git client.

Get git if you haven’t already:

http://git-scm.com/

Once that’s installed just run the following from a command line:

git clone git://github.com/username/project.git

To get the connect-auth source tree use:

git clone git://github.com/ciaranj/connect-auth.git

Enjoy your new source.

Remove ^M with vi AKA bad interpreter: No such file or directory

Got a script with ^M at the end of each line because the file came from a dos machine?  With perl you’ll get something like:

/usr/bin/perl^M: bad interpreter: No such file or directory

First of all open this file in vi in binary mode:

vi -b yourscipt.file

Then after typing colon type the following:

%s/^M//g

NOTE. To get the ^M do NOT use ^ and M.  Use control+V then immediately control+M.

Then [enter].  Then to save and exit:

:wq [enter]

Job done!

MySQL [lack of] performance

I certainly don’t profess to be a MySQL performance tuning expert. What I am is a long-time MySQL lack of performance sufferer. In fact, writing this I’m in the process of waiting for several table deletes and MySQL WorkBench has frozen (again).

Anyway, here’s some thoughts:

  1. If you have heavy writes bear in mind this severely affects locking for MyISAM tables.  One thing that sucks with MySQL table engines is you trade off performance (MyISAM) against granular locking (InnoDB).
  2. Try to avoid tables getting too large to manage.  If you’re storing historical data write an archive / cull script early on to keep the database manageable.
  3. Use SHOW PROCESSLIST to see what’s running if you’re experiencing problems.  Processes marked ‘locked’ or ‘freeing items’ are bad.
  4. If you get into trouble and need to delete all the rows in a table, use TRUNCATE TABLE [tblname] rather than DELETE FROM.  This is significantly quicker.  (Thanks Rainer for that one.)
  5. Most obviously – use indexes judiciously.  Adding indexes slows down insert speed to allow better retrieval rate.  If the tables are too index heavy the insert speed will slow down such that it causes more locking.

Regex to trim number to two decimal places

Not very generic, but at least easy to understand:

m/^(.+?\.[0-9]{2})/;

123.456789 becomes 123.45