Jump to content

This is for Comment


This topic is 6718 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Below is a CF that you kindly re-wrote for me. It has a small, but annoying bug. Here's the CF..it formats phone numbers:

Let ( [

input = Filter ( phone ; "0123456789" ) ;

formatChar = Case ( Length ( format ) ; Right ( format ; 1 ) ; "#" ) ;

inputChar = Right ( input ; 1 ) ;

nextFormat = Left ( format ; Length ( format ) - 1 ) ;

nextFormatChar = Right ( nextFormat ; 1 ) ;

nextInput = Left ( input ; Length ( input ) - ( formatChar = "#" ) )

] ;

Case ( Length ( nextInput ) or Length ( input ) and nextFormatChar ≠ "#" ; PhoneFormat ( nextInput ; nextFormat ) )

&

Case ( formatChar = "#" ; inputChar ; formatChar ))

It works fine if the phone # has an area code. IF the format is '(###) ###-####' and the phone number has no area code, the function inserts a leading space which it shouldn't. While I can use Trim in the calculation to remove the space, it really should be embedded in the CF (which I did, altho not in the above version).

However, I prefer to use (for space saving considerations)

'###-###-####', which without an area code leaves a leading '-'. I don't seem to be able modify the CF to remove it. Could I impose on you to modify the CF?

Thanks, Steve

Link to comment
Share on other sites

Hi

while you wait for comment, try this:

Let ( [

input = Filter ( phone ; "0123456789" ) ;

formatChar = Case ( Length ( format ) ; Right ( format ; 1 ) ; "#" ) ;

inputChar = Right ( input ; 1 ) ;

nextFormat = Left ( format ; Length ( format ) - 1 ) ;

nextFormatChar = Right ( nextFormat ; 1 ) ;

nextInput = Left ( input ; Length ( input ) - ( formatChar = "#" ) )

] ;

Case ( Length ( nextInput ) ; PhoneFormat ( nextInput ; nextFormat ) )

&

Case ( formatChar = "#" ; inputChar ; formatChar ))

Link to comment
Share on other sites

I'm afraid I don't have a good GENERAL solution for this. You see, the function goes from right to left, and compares the phone number digits to the format characters. At some point, it runs out of digits, but the format may still have some characters left.

In order to do what you want, the function would have to make a decision: is the next character a WRAPPING character, such as "(" - in which case it should be included, or is it a SEPARATOR, such as a space or a hyphen - in which case it should be ignored.

At present, the function has no information enabling it to make such decision. It treats all characters (other than #) alike - and when the digits run out, it adds one more character (if any are left). Otherwise, it would never add the leftmost "(" in "(123) 456-7890".

It's possible to add exceptions to the rule above - but these would need to be hard-coded, so the format parameter would not be entirely user-defined. (I don't think adding a parameter for the user to fill-in those exceptions would be practical.)

Out of curiosity, I checked how other applications treat this issue. I don't have Excel installed, but it's supposed look-alike NeoOffice returns 1234 formatted as (###) ###-#### as "() -1234". Apple's AddressBook will not format the number at all, unless it has exactly the prescribed number of digits.

That said, here are two possible modifications. I believe both will perform the same with your format of preference - the difference between them is how they handle parenthesses in the format.

// PhoneFormat ( phone ; format )

Let ( [

input = Filter ( phone ; "0123456789" ) ;

formatChar = Case ( Length ( format ) ; Right ( format ; 1 ) ; "#" ) ;

inputChar = Right ( input ; 1 ) ;

nextFormat = Left ( format ; Length ( format ) - 1 ) ;

nextFormatChar = Right ( nextFormat ; 1 ) ;

nextInput = Left ( input ; Length ( input ) - ( formatChar = "#" ) )

] ;

Case (

Length ( nextInput ) or Length ( input ) and nextFormatChar = "(" ;

PhoneFormat ( nextInput ; nextFormat ) )

&

Case ( formatChar = "#" ; inputChar ; formatChar )

)

---

// PhoneFormatP ( phone ; format )

Let ( [

input = Filter ( phone ; "0123456789" ) ;

formatChar = Case ( Length ( format ) ; Right ( format ; 1 ) ; "#" ) ;

inputChar = Right ( input ; 1 ) ;

nextFormat = Left ( format ; Length ( format ) - 1 ) ;

nextInput = Left ( input ; Length ( input ) - ( formatChar = "#" ) )

] ;

Case (

Length ( nextInput ) or PatternCount ( nextFormat ; "(" ) ;

PhoneFormatP ( nextInput ; nextFormat )

)

&

Case ( formatChar = "#" ; inputChar ; formatChar )

)

Edited by Guest
Removed an unrequired line from the second function
Link to comment
Share on other sites

Note that when you change the name of a recursive custom function, you also need to change to call to itself in the function itself. I am guessing that's why Daniele's modification didn't work for you.

Daniele, your fix works for the specific format - but will backfire with parenthesses.

Link to comment
Share on other sites

Yes - but for every yes there is a but...

If the function only works with a specific format, what's the point in having the format as a parameter?

I'd think that if you have a function that enables the user to define the format, then the function should work correctly with any reasonable format.

If the function is designed to work with a single format, then there's no need for recursive calculation. A simple Replace() or Substitute() can do quite nicely, as I have already pointed out in the original thread.

Link to comment
Share on other sites

I'd think that if you have a function that enables the user to define the format, then the function should work correctly with any reasonable format..

This statement reminded my of a file back in v3/4 days called "Phone Format Phun..." By: Parker Bennett, you can download a copy of it, and covert it to current versions. from here:

http://www.fmfiles.com/tnt2.html

Lee

Link to comment
Share on other sites

This topic is 6718 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.