Archive for March, 2010

Classic Programming Error: Variables inside a loop

March 25th, 2010 No comments

This for loop is OK

foreach($cargo_data as $cid=>$cargo_record)


But then later you add a condition cargo_id>6

foreach($cargo_data as $cid=>$cargo_record)
if ($cid>6) {$cargo_id=$cargo_data[$cid][‘cargo_id’];}


Now the bug is that if $cid is less than 6 $cargo_id is not set and will likely inherit the value it was set to in the last loop this type of error may not be immediately noticeable but can cause chaos. In this simple example it is easy to see that their should be an else clause, however the real solution is to systematically initialize all the variables at the beginning of the loop.

Categories: Coding Tips Tags:

Converting a Textified File Name back into Title Case

March 11th, 2010 No comments

This could easily be done with Perl but here is a zsh solution

I want to convert a textified string to a Title Case Phrase
> eg
> fred-goat-dog.jpg to Fred Goat Dog

% print ${${(Cs:-:):-fred-goat-dog.jpg}%.*}
Fred Goat Dog

This curiously uses a smiley face though
echo ${(C)foo:gs/-/ /:r} might be more elegant
From the zsh newsgroup (RD&MM)

and to textify a phrase

s=’Fred Goat Dog’
print ${(L)s:gs/ /-/}.jpg
print ${(L)s// /-}.jpg

Categories: zsh Tags: , ,

Useful VIM Abbreviations for Debugging Perl

March 5th, 2010 No comments
iab perlb print “<p>debug ::: $_ :: $’ :: $` line “.__LINE__.”\n”;exit;
iab perlbb print “<p>debug ::: <C-R>a line “.__LINE__.”\n”;exit;
iab perlbd do{print “<p>debug :: <C-R>a line “.__LINE__.”\n”;exit} if $_ =~ /\w\w/i;
iab perld use Data::Dumper; print Dumper %ENV;

the <C-R>a automatically inserts whatever variable you had previously stored in register a

Categories: Uncategorized Tags: