Thursday, February 16, 2006

Some Favorite, Frequenty-Used Functions

As you know, I usually add something new to this blog every other day on the average. Why the delay this time? The main reason is that I have Verizon high-speed DSL (or, rather that I was without Verizon DSL for five days).

I phoned Verizon on Thursday to let them know that I was unable to connect to the Internet. I was told that I would have service restored "in 48 to 72 hours." How does that become five days? Simple. Verizon does not work on the weekends. (They presumably operate following the non-Biblical injunction, "Five days shalt thou labor...." It's not enough for them to take Sunday off, they apparently need to take Saturday off as well.)

So I was without Verizon DSL for five days. I was given an "open ticket," but whenever I called up to check on it, all I got was a recorded message telling me that they were "still working on my problem" and that "no further information was available." That is, that's what I got when I got a response other than "we're experiencing a greater-than-usual call volume at this time. Please hang up and try your call again later." (That's after you go through the voice-driven automatic menu system, where you're told, "I don't understand. Please answer 'Yes' or 'No.'" either because you don't respond because it's not really a "yes/no" type question or because you do respond but Verizon's voice recognition software doesn't "understand.")

I did finally get through to talk with a genuine human being five days later and got to talk with a technical support person who walked me through the procedure to get myself connected to the Internet again. (No explanation was given as to why this could not have been done five days earlier, and the implication was given that I must have pressed the reset button on my modem, which I know is something that I did NOT do.) Since I get hundreds of emails a day, needless to say, I was not very happy about being off-line for five days, but perhaps my experience with Verizon explains why I have not been able to add a new entry to this blog until now.

I hope to have a new program or two available to you by tomorrow, but for now let's briefly consider the topic of "a few functions I've added to REALbasic, since I use them so often." (If you have such a list, I'd be very interested to see it.)

I think all of us probably have such routines, perhaps gathered together in a module that we include in our Projects. (I'm not quite that organized yet, but there are some things I find myself regularly adding to Projects, so I should probably get around to putting them into a module to make them more readily accessible.)

What are some of my useful add-on subs and functions? Here's one:

Function Mid1 (M As String, Pos1 As Integer, Pos2 As Integer) As String
   Return MidB (M, Pos1, Pos2 - Pos1 + 1)
End Function

I often prefer this to the regular Mid or MidB, because I often have in mind when doing a string extraction the starting position and the end postion, not the starting position and how many characters. For those people who don't have to even think about "Pos2 - Pos1 + 1," such a function is not needed, but I personally find it helpful.

When I want to find the first occurence of StringA or StringB within StringC (and I want whichever comes first, whether it be StringA or StringB), I use this function, assuming that A = InStrB(StringC, StringA) and B = InStrB(StringC, StringB):

Function MinNonZero (A As Integer, B As Integer) As Integer
   If A <> 0 And B = 0 Then
      Return A
   ElseIf A = 0 And B <> 0 Then
      Return B
   ElseIf A <> 0 And B <> 0 Then
      Return Min (A, B)
   ElseIf A = 0 And B = 0 Then
      Return 0
   End If
End Function

I'm sure that the following could be written much more efficiently, but sometimes I want to create a longer string based on repetitions of a shorter string. Then I use this:

Function Rpt (A As String, B As Integer) As String
   Dim I As Integer
   Dim M As String
   M = ""
   For I = 1 To B
   M = M + A
   Next I
   Return M
End Function

Or, if I want to count how many instances there are of a shorter string within a larger string, I use this:

Function HowMany (A As String, WhatToCount As String) As Integer
   Dim Counter As Integer
   Counter = CountFields (A, WhatToCount) - 1
   If Counter = - 1 Then Counter = 0
   Return Counter
End Function

Or, when I want to find the position of not the first occurence of a shorter string within a longer string, but the last occurence, I use this:

Function InStrBRev (Pos1 As Integer, String1 As String, String2 As String) As Integer
   Dim I As Integer
   I = Pos1
   Do
      I = I - 1
   If I = 0 Then Exit
      If MidB (String1, I, Len (String2) ) = String2 Then Exit
   Loop
   Return I
End Function

One function I use surprisingly often (perhaps partly because of working with CodeHelper and related programs is this one:

Function OutsideQuotes (s1 As String, Pos1 As Integer) As Boolean
   Dim A As String
   Dim Counter As Integer
   A = LeftB (s1, Pos1 - 1)
   Counter = HowMany (A, Q)
   If Counter Mod 2 = 0 Then
      Return True
   Else
      Return False
   End If
End Function

The routine determines whether the character at a certain position within a string is "inside quotation marks" or "outside quotation marks."

Many of these routines, you may have noticed, involve string handling, and more efficient versions of some of these can be found in Joe Strout's StringUtils module at http://www.strout.net/info/coding/rb/intro.html
"This is a public-domain module of string functions, including ways to reverse a string, remove or delete certain characters, convert a string into hex, handle fields containing quotes, measure the similarity of two strings, and more.

The archive contains an RB project file which runs the unit tests on the various StringUtils functions. Just drag the StringUtils module out of this project, then drag it into your own projects, and enjoy.

I wrote most of mine before that package was created, so mine do not take advantage of the more efficient procedures to be found there.

Barry Traver



Home Page for This Blog: http://traverrb.blogspot.com/

Programs and Files Discussed in the Blog: http://traver.org/traverrb/

0 Comments:

Post a Comment

<< Home