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)
Outputs:
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 family.com.nokia.series40.3rdEd.bus.bt.

The image for the phone was downloaded from ericfish.com and copied into the /Applications/iSync.app/Contents/PlugIns/ApplePhoneConduit.syncdevice/Contents/PlugIns/PhoneModelsSync.phoneplugin/Contents/Resources/ folder.

The following code was merged into the MetaClasses.plist file in /Applications/iSync.app/Contents/PlugIns/ApplePhoneConduit.syncdevice/Contents/PlugIns/PhoneModelsSync.phoneplugin/Contents/Resources/

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:

Clang!

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.
Example:

Input Output

2 2 4

3 2 5

These are what I posted as solutions:

Prompts version

::aplusb.cmd
@echo off
setlocal
set /p a="A: "
set /p b="B: "
set /a c=a+b
echo %c%
endlocal

All on the commandline version

::aplusb.cmd
@echo off
setlocal
set a=%1
set b=%2
set /a c=a+b
echo %c%
endlocal

Formula on the command line version

::aplusb.cmd
@echo off
setlocal
set /a c=%~1
echo %c%
endlocal

Example of 'Formula on the command line version'

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

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

::aplusb.cmd
@echo off
setlocal
set /p a="Input stream: "
call :add %a%
echo %res%
endlocal
goto :eof

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

Example of 'parse the input stream version'

>aplusb
Input stream: 1234 5678
6912
>aplusb
Input stream: 123 234 345 456 567 678 789 890
4082

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.

Saturday, December 12, 2009

[Protium] PLEAC 1.0

<@ OMT>-----------------------------</@>

<@ LETVARLIT>string|\n</@>
<@ OMT>two characters, \ and n</@>
<@ LETVARLIT>string|Jon 'Maddog' Orwant</@>
<@ OMT>literal single quotes</@>
<@ OMT>-----------------------------</@>

<@ LETVARLIT>string|
</@>
<@ OMT>a "newline" character</@>
<@ LETVARKEY>string|__Newline</@>
<@ OMT>a "newline" character</@>
<@ LETVARLIT>string|Jon "Maddog" Orwant</@>
<@ OMT>literal double quotes</@>
<@ LETVAREXPLIT>string|Jon &pipe;Maddog&pipe; Orwant</@>
<@ LETVAREXPLIT>string|Jon <&prot;>Maddog</&prot;> Orwant</@>
<@ OMT>Because | and <@ and </@ are significant there are ways to quote them</@>
<@ LETVARLIT>Bruce Goose|布魯斯鵝</@>
<@ OMT>a variable name containing spaces.
A variable containing Unicode.
Programming code in Unicode.</@>
<@ OMT>-----------------------------</@>

<@ OMT>Multi-line strings.
There is no equivalent to perl's "here" documents in Protium</@>

<@ LETVARLIT>a|
This is a multiline string
that spans 3 lines (it contains 3 newline characters).
</@>

<@ LETVARLIT>string|This is a multiline string
that spans 2 lines (it contains 1 newline character).</@>

<@ OMT>-----------------------------</@>

[Protium] PLEAC in Protium

PLEAC, for those unfamiliar with it, is the Programming Language Examples Alike Cookbook. This is a brilliant site which takes examples of Perl, given in Perl Cookbook by Christiansen and Torkington, and invites contributors to demonstrate how other languages implement the same functionality. Many languages are in the process of being compared and contrasted in this way, including Python, Ruby, Tcl and Haskell. All manner of functionality is covered, from Strings, Numbers, Dates and Times through to Internet Services, CGI Programming and Web Automation.

For the next few postings I am going to do a PLEAC for Protium. You won't find Protium on PLEAC's pages because PLEAC is limited to open-source languages. Protium is proprietary and closed-source (at present.)

The challenge with converting from Perl to Protium is similar to that faced by linguists translating from one human language to another: do you translate the sense of the utterance, or do you just translate word for word. For example, the Tok Pisin word rabisman literally means "rubbish man". However, it is almost never used that way. Instead it often carries the sense of "fool" or "good-for-nothing." So when converting the Perl to Protium, I've tried to give the sense of the Perl, rather than follow it line for line or word for word.

There will be the odd non-PLEAC posting, but I will try to work my way through the entire PLEAC, all 300K's worth.

Wednesday, October 14, 2009

[Jabaco] JaCOB and WScript.Network

I wonder about myself sometimes. Am I sane? For instance, I’ve taken recently to learning and developing with Jabaco. This would seem from one angle, good, and from another, daft. 

Jabaco is a simple programming language with a Visual Basic like syntax. Jabaco enables you to create powerful software for all Java supported operating systems.

So there you have it: a VB6-alike (more or less) syntax targeting the JVM. In my experience, people bagging VB syntax are in the majority. People bagging Java aren’t infrequent either. And I know Java about as well as I know Babylonia Cuneiform.

That said, it’s been a bit of fun figuring things out in Jabaco, and discovering just how effectively one can hook into the Java subsystem.

The code below demonstrates The JACOB Project: A JAva-COM Bridge talking to WScript.Network. Comments are a bit sparse, but VB folk should be able to figure it out quick enough.

Public Sub main(ByJava args() As String)
   Dim myArgs() As String
   myArgs = args
   Dim oShell As ActiveXComponent
'ActiveXComponent is exported by JACOB and referenced in the IDE
'(yes, there’s an IDE, and it’s pretty good too) Set oShell = New ActiveXComponent("WScript.Network") Dim a As String Dim b As String Dim c As String Dim dShell As Dispatch
'Dispatch also part of JACOB. Nice that it uses the standard jargon Set dShell = oShell.getObject() a = Dispatch.call(dShell,"UserDomain") b = Dispatch.call(dShell,"ComputerName") c = Dispatch.call(dShell,"UserName") MsgBox (a & ", " & b & ", " & c) comthread.Release() End Sub

The IDE makes possible ‘compiling’ to an EXE. The code above compiles to about 400K. Of course, the presence of a JRE is implied. And it runs, nicely.

I think I’ll be spending more time with Jabaco. It’s got a lot of promise, especially for the VB-deranged like me.

© Copyright Bruce M. Axtens, 2009.

Thursday, July 02, 2009

[VB6] Using VBScript's Escape and UnEscape

Something again from an answer I gave on StackOverflow. This one demonstrates how to use MSScriptControl.ScriptControl to make VBScript's Escape and Unescape functions available to the VB6 programmer.

The best thing would be add MSSCRIPT.OCX to the project, but for the sake of demonstration, I'll use CreateObject instead.


Using the code is easy.


It's that simple. And the technique can be used to get at other VBScript functionality. In fact, .Language can have values other than "VBScript", making it possible to interface to any language with a Windows Scripting Host presence.

© Copyright Bruce M. Axtens, 2009

Tuesday, May 19, 2009

[VB6] MAXDOUBLE, MINDOUBLE, +INFINITY, -INFINITY and NaN

VB6 doesn't seem to be able to specify that a Double contains MAXDOUBLE, MINDOUBLE, +INFINITY, -INFINITY or NaN. (I could be wrong on that as there may be some undocumented feature hiding in there somewhere.)

Anyway, I came up with a way of storing these values into Doubles so that they could be used in a Complex Numbers library I've been writing.

Essentially, I create an 8 byte array, load it up with the relevant values, and then, using API calls and the VarPtr function, store the contents of the array into the storage used by the Double.

Here's the code. First the declarations.


Then the routine that does the work.


Finally, a slice out of the Complex Numbers project demonstrating the use of some of these declarations.


Enjoy!


© Copyright Bruce M. Axtens, 2009

Thursday, May 14, 2009

[VB6]Searching an ActiveX/COM object for a method

This article was first posted by me on StackOverflow in response to my own question, "How do I search an ActiveX/COM object for a method?". This article is an edited version of the original.

"After Googling around not quite finding what I wanted, I remembered the
Edanmo site which got me thinking about TLBINF32.DLL, downloading Microsoft's TLBINF32.CHM and reading up on GetMembersWithSubStringEx.

Below is the implementation of it (done in VB6 with a reference to TLBINF32.DLL), some demo VBScript and output, and the wrapping of that functionality in some VBA.


VBScript demo. The above code was included in my StdLib DLL in the Registry coclass.


Output from the demo (script was run in SciTE).


Finally, the VBA code. A cell has a symbol in it and this routine finds it or returns an error string. (reg. in this case refers to the Registry coclass in the StdLib.DLL)


Hmm ... no error checking. Should fix that.

© Copyright Bruce M. Axtens, 2009

Sunday, April 12, 2009

[newLISP] restart-router

The boss said I'd find Lisp addictive, and he was right.

The following is a
newLISP implementation of a project attempted in VB6. The VB6 one failed to work consistently. The newLISP one hasn't failed yet.

The situation is that our ADSL connection goes down at random times, necessitating a walk down the hall to the comms room, wherein one turns off the power to the router so that it resets. I found out during the week that if I telnet into the router, give the appropriate password and the enter 'restart', that'll restart the router. Will it reduce the need to walk to the comms room? I really don't know as, all of a sudden, the router is working fine.

Here's the code:
I really like the fourth parameter on the net-receive call; not only do I receive but I can look for something in what is received. I suppose testing for not receiving it would be good, but I'll leave that for another time.

Once the restart command is sent, there's no need to attempt a net-receive. Just close the connection. Note also the easy way of referring to and using a function in a DLL.

Using the link.lsp script (which comes in the standard newLISP install) I've been able to turn this code into a standalone EXE, and once I've established that it really does do the job, I'll install it on a couple of other machines in the office.

I suppose we actually need the exercise, but maybe we don't need the aggravation.

By the way, my last newLISP posting has generated an interesting conversation on
comp.lang.misc (also mirrored on comp.lang.lisp).

© Copyright Bruce M. Axtens, 2009

Thursday, April 09, 2009

[newLISP] reverse-find

I've always thought learning Lisp would be a good thing to do. The purists may argue that newLISP is not the best place to start. Oh well, too bad.

Below is my first (ever) lambda expression, a port of the VBScript RevInstr() function.


I must say I'm impressed with newLISP. Note that the minus (-) function above can receive more than one argument. A lot of the functions are like that.

Using the reverse-find is very like the (find) function, as below



One thing that differentiates my lambda expression from the in-built find, is in the manner in which an error is flagged.



I'm not sure at this point why the difference and what to do about it. In my use of (find) I check for a result equal to nil, but with (reverse-find) I have to check to see if the result is a string. For example,



Doubtless, someone in the
newLISP Fan Club will set me straight soon.

© Copyright Bruce M. Axtens, 2009

Friday, November 28, 2008

[Shameless Plug] Resistance Is Useless (an iPhone / iPod Touch app)

A good friend of mine has recently developed an iPhone / iPod Touch app for decoding the colour bands of 4, 5 and 6 band resistors. Check it out!



© Copyright Bruce M. Axtens, 2008

Monday, November 03, 2008

[Networks] How to calculate a netmask

To say that I've been busy of late is somewhat of an understatement. Mind you, I've had time to create a presence over on StackOverflow, and to start "playing" MouseHunt on FaceBook.

I was responding to something over on StackOverflow: "
What’s the best way to convert from network bitcount to netmask?" and added my two-bob to the mix with the following bit of VBScript.
A few examples runs are as follows:
It's been ages since I did the Cisco coursework (never did sit the exam) so I'm not sure if it's totally correct.

The binary2hexadecimal and binary2decimal functions were interesting to write as I was trying to find simpler, more lateral ways of doing things.
© Copyright Bruce M. Axtens, 2008

Monday, July 28, 2008

[Delphi] Updating SysTray

I'd been lurking at ITtoolbox for a while and saw this request for help with updating the system tray.

I've had similar problems myself. For instance, if you use boost or some similar process-control tool to kill the Skype.exe and SkypePM.exe programs, the processes disappear from memory but the system tray still shows the Skype icon until you move the mouse over the icon. Then the tray refreshes, the Skype icon disappears and the remaining icons rearrange.

On
Experts Exchange I found a bit of Delphi code which moves the mouse over the systray (using system messages rather than moving the mouse pointer itself.) This convinces Windows to update the systray. Because not everyone has an Experts Exchange membership, I've turned the code into a COM DLL, which you can download. Source is included (Artistic2 license.) A copy of the original Experts Exchange code can be found here.

Once you've registered the DLL (regsvr32 UpdateSysTray.DLL) you can use it in anything that supports ActiveX/COM objects. For example, in VBScript:


© Copyright Bruce M. Axtens, 2008