Formatting phone numbers

I have a file containing phone numbers. There is one phone number in a record, and that phone number is assigned a “type”. The phone number type, PHO:PhoneType, is a byte, and is selected from a drop list with the following options:

‘Mobile|#0|Work|#1|Home|#2|Work Fax|#3|Home Fax|#4|Direct|#5|Reception|#6|After Hrs.|#7|13 Number|#8|1300 Number|#9|18 Number|#10|1800 Number|#11|Other|#12’

The phone number field, PHO:Number, is a string of 20 characters.
I have the following routine attempting (unsuccessfully) to format the displayed phone number depending on the type.

PhoneNumberFormat   ROUTINE
	CASE PHO:PhoneType
	OF 0			! Mobile
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P????-???-???P)
	OF 1			! Work
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P??-????-????P)
	OF 2			! Home
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P??-????-????P)
	OF 3			! Work Fax
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P??-????-????P)
	OF 4			! Home Fax	
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P??-????-????P)
	OF 5			! Direct Line
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P??-????-????P)
	OF 6			! Reception
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P??-????-????P)
	OF 7			! After Hours
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P??-????-????P)
	OF 8			! 13 Number
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P??-??-??P)
	OF 9			! 1300 Number
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P????-???-???P)
	OF 10			! 18 Number
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P??-??-??P)
	OF 11			! 1800 Number
		PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P????-???-???P)	
	OF 12
		PHO:Number = FORMAT(PHO:Number,@S20)
	END
	DISPLAY()

The routine is called when the PHO:PhoneType is Accepted.

It is my intention that the PHO:Number should be stored as a continuous string for sorting purposes, and that the Picture attribute is used only for display.

What I have currently is storing nothing in the PHO:Number field. I am using Clarion 9 with TPS files.

Any suggestions would be most welcome.

Thanks in advance,
Kenn Sharples

I’ve always had problems with trying to format phone numbers. Turns out it’s difficult and not everyone agrees. In Australia almost all our phone numbers are 10 digits. except when they aren’t. we have some special 6 digit ones. Our mobile number all have the area code of 04 and most people format that as nnnn nnn nnn but some (like me) prefer nn nnnn nnnn for everything.
Then you have international numbers where you need to add the country code. so +nn nnn nnnn nnnn ???

That all just makes formatting and sorting challenging, so I don’t. I have a S20 string field and let my user use their prefered formatting scheme.

1 Like

Kenn,

Look at StringTheory,

dfmt StringDeformat

sPhone = dfmt.deformatPattern(PhoneNumber,‘@p(<<<###) ### ####’) ! RETURNS 0878280123

Seems you are using DEFORMAT in a wrong way.

1 Like

AFAIK, pattern pictures (@PP) don’t use the ‘?’ token…

Use # for @P numbers and no @s20 picture with Deformat.

PHO:Number = FORMAT(DEFORMAT(PHO:Number),@P####-####-###P)
			

This tool on GitHub will let you test Format and Deformat with any picture so you can know how it will work without compiling.

The screenshot shows @N pictures but you can use @P as seen on the bottom right.

image

6 Likes

Since EVERYTHING you’re passing to format is the exact same DEFORMAT, you can break up your code to make it easier to test.

Your first line could be

Deformatted = DEFORMAT(PHO:Number,@s20)

Now you can test for the value of what you get from your DEFORMAT(). Then you’ll find that it isn’t doing what you want.

So perhaps, in your testing, you try it without the @S20 because now you’ve re-read the docs for DEFORMAT and have seen the examples.

Now you can pass your Deformatted to FORMAT with more confidence about what is transpiring, and make more maintainable code as a bonus.

1 Like

Never had a user complain about that either.

Good idea do everything by calling a
PhoneEntered PROCEDURE(*STRING inOutPhone, STRING inPicture)

Then you could add features like tell the user he did not enter enough digits by counting the # in the picture.

I adjusted
PHO:Number = FORMAT(DEFORMAT(PHO:Number,@S20),@P????-???-???P)
to read
PHO:Number = FORMAT(DEFORMAT(PHO:Number),@P####-####-###P)
in my routine, and now, all is formatting nicely.

Thank you, CarlBarnes and all others that responded.
Regards,
Kenn Sharples

When help in performing a task is requested, simply saying, “You’re doing it wrong,” is of no help at all.

You’re right, I should have added “read the docs”.