Sunday, December 19, 2010

[Javascript] Wildcard string matching / globbing, Take 2.

Okay, I'm convinced: you just can't beat regular expressions. (Okay, SNOBOL4's pattern matching is streets ahead, but there's nothing out there like that for Javascript, AFAIK.)

I've reworked the matchesWild function, caling it grepWild. It takes the wildCard parameter as before but instead of all the substr stuff, it replaces '?' with '.', '*' with '.*' and wraps with '^' and '$'. Then it feeds that into match().

It's simpler, easier on the eyes, and faster too.
© Bruce M. Axtens, 2010

Tuesday, November 30, 2010

[Javascript] Wildcard string matching / globbing

Most Javascript programmers are more than happy with regular expressions. However, there are times when something like wildcards ('?' for one character and '*' for many) would be helpful.

Jack Handy's first and last (it seems) article on CodeProject implemented a wildcard string compare (also known as 'globbing') function in C. Below I present my port of that function to Javascript.

As a newbie Javascript programmer (4 weeks in as of 2010-11-30), I've pleasantly surprised by the expressive power of Javascript. What has caught my eye recently is the power of .prototype., so this implementation of matchesWild() is declared as a prototype extending the String class.

Here's the code, with some commentary:
Apart from the comments, notice the way Javascript adds a method to an object, defining an anonymous function with, in this case, one parameter, and assigning it as a prototype.
The C version did all kinds of interesting things with pointers. Javascript doesn't have that kind of stuff (AFAICT), so I had to do the equivalent with numeric offsets and substr().

Perhaps there are better ways of doing this. I have tried to do this kind of thing before, but this code seems to do a pretty good job of it and hasn't failed thus far.

What should be noted, and perhaps dealt with, is that there is no explicit check for calling the method without a parameter. If one does this, an error is raised. Chrome's V8 raises "TypeError: Cannot call method 'substr' of undefined".
And now some examples (evaluated using macports's JavaScript-C 1.7.0 2007-10-03)
More Javascript coming soon. In the meantime, enjoy.

© Bruce M. Axtens, 2010

Tuesday, November 16, 2010

[iSync] Nokia 5000d-2

After looking around for a while I finally figured out how to get Apple iSync to talk reliably to my Nokia 5000d-2. Most of the other 'solutions' assume that the 5000 is a Series 60 phone. It is in fact a series 40 phone, thus the

The image for the phone was downloaded from and copied into the /Applications/ folder.

The following code was merged into the MetaClasses.plist file in /Applications/

I hope someone finds this helpful. It certainly makes things a bit easier for me. Now all I have to do is figure out how to manage the SMS subsystem. Gammu looks promising, once I figure out the link error. More on that another time.

© Bruce M. Axtens, 2010

Tuesday, September 14, 2010

[VBA] Something like COUNTIFS for Excel 2003

Well, there you go. A colleague sends you a copy of an Excel file and needs a nice formula. So I do the nice formula. Then I try to save the sheet back into the same form in which it arrived:


Seems that Excel 2003 doesn't have the nice little COUNTIFS function that I have in my Excel 2007. What to do, what to do, what to do ...

Well, it'd be nice to install Office 07, but in the interim, how about a bit of VBA?

We'll call it RANGEDCOUNT, and it will accept a range of data, and a string being the criteria. The criteria can be
  1. a single value preceded by an operation (>, >=, =, <=, <, #), or

  2. a range of values, the upper and lower bounds separated by a dash.

Right, that's all the declarations over with. Now to check whether we have a range specification or not and what to do if it's not. Notice the 'Instr(2' which checks for an equals sign after the first character, as we're checking to see if we have '>=' or '<=' and don't want to fail on a bare equals
The else fires if we do in fact have a range setting and handles that appropriately.
Next, step through the data and for each element, apply the logic for either a ranged count or an operator count.
Okay, iCount should have the result, so put it in RANGEDCOUNT and end.
Not bad for 15 to 20 minutes work, and would've been faster if I'd remembered how to get data out of Ranges. Now my colleague is happier and I've had a chance to do some something interesting. Granted, it's not particularly fault tolerant, but it's enough for now. © Bruce M. Axtens, 2010

Wednesday, April 28, 2010

[Batch File] Rediscovering CMD.EXE batch scripts

Insanity must be setting in; I’ve been trying to solve RosettaCode tasks with Windows CMD.EXE batch scripts. For example, their A+B task:

A+B - in programming contests, classic problem, which is given so contestants can gain familiarity with online judging system being used.

A+B is one of few problems on contests, which traditionally lacks fabula.

Problem statement Given 2 integer numbers, A and B. One needs to find their sum.

Input data
Two integer numbers are written in the input stream, separated by space.
(-1000 \le A,B \le +1000)
Output data
The required output is one integer: the sum of A and B.

Input Output

2 2 4

3 2 5

These are what I posted as solutions:

Prompts version

@echo off
set /p a="A: "
set /p b="B: "
set /a c=a+b
echo %c%

All on the commandline version

@echo off
set a=%1
set b=%2
set /a c=a+b
echo %c%

Formula on the command line version

@echo off
set /a c=%~1
echo %c%

Example of 'Formula on the command line version'

>aplusb 123+456
>aplusb "1+999"

Parse the input stream version (thanks to Tom Lavedas on alt.msdos.batch.nt)

@echo off
set /p a="Input stream: "
call :add %a%
echo %res%
goto :eof

set /a res=res+%1
if "%1" neq "" goto :add

Example of 'parse the input stream version'

Input stream: 1234 5678
Input stream: 123 234 345 456 567 678 789 890

Batch files are a bit arcane, it’s true, but the extensions added post Windows 2000 make it a lot more entertaining.

Wednesday, February 10, 2010

[VBScript] Small contributions to RosettaCode

I recently discovered RosettaCode. The initial blurb from the site is as follows:

Rosetta Code is a programming chrestomathy site. The idea is to present solutions to the same task in as many different languages as possible, to demonstrate how languages are similar and different, and to aid a person with a grounding in one approach to a problem in learning another. Rosetta Code currently has 370 tasks, and covers 195 languages, though we do not (and cannot) have solutions to every task in every language.

A variety of tasks are listed, and visitors to this site are invited to solve the tasks in the language of their choice. The tasks cover everything from the mundane Empty Program to the classic Towers of Hanoi, the practical User Input, the mathematically-inclined Lucas-Lehmer test, and the involved yet entertaining RCRPG.

I have made some small contributions to its collection of VBScript programs, as below:

99 Bottles of Beer Array concatenation Assertions Delete a file Execute a Markov algorithm Fibonacci sequence Flatten a list Function definition Generic swap Palindrome detection Pangram checker Program termination Sorting algorithms/Gnome sort Tokenize a string

Feel free to edit what I’ve done and add your own in whatever language takes your fancy.

© Copyright Bruce M. Axtens, 2010.