Complex Expression Length() issue?

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
DreamTheater
Newbie
Posts: 19
Joined: Mon Oct 14, 2019 6:23 am

Complex Expression Length() issue?

Post by DreamTheater » Wed Dec 18, 2019 4:31 am

G'day,

I may have found a bug with how the Length() function within complex expressions works.

In this case I'm extracting a phone number from an Array (created by CSVFileToArray) which may be either 9 or 10 digits.
The value should always be 10 digits (Australian mobile number starting with 04) however Excel formatting may drop the leading 0 causing it to be 9 digits long.
I want to check the length, if the result is 9 then add a 0 to the start, otherwise continue.

The script I've used to test is:

Code: Select all

Let>TEST_ARRAY_2_0=400123456

If>{Length(%TEST_ARRAY_2_0%) = 9}
    Let>MSN=0
    ConCat>MSN,TEST_ARRAY_2_0
ELSE
    If>{Length(%TEST_ARRAY_2_0%) = 10}
        Let>MSN=TEST_ARRAY_2_0
    ELSE
        //MSN not found in .csv - exception?
    EndIf>
EndIf>
In the above example it works as expected.
If there is no Excel formatting and the leading 0 is present (TEST_ARRAY_2_0=0400123456) the length result is still 9.
If I instead change TEST_ARRAY_2_0=4001234560 it counts as 10 characters.

It seems that if a variable starts with a 0 the length is -1 of what it should be.

The Length> command still works fine, so as a workaround I'll use this instead of the complex expression for this scenario.

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1380
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Complex Expression Length() issue?

Post by Dorian (MJT support) » Wed Dec 18, 2019 1:30 pm

Your Complex Expression and Length is working, but I am wondering if Concat is doing what you expect it to?

What you're doing here is creating a variable named MSN, which is a zero followed by TEST_ARRAY_2_0

I added a MessageModal so you can see it in action, but of course you can also use F8 to step through it line by line and see the variables in the Watch List in the left window pane.

Code: Select all

Let>TEST_ARRAY_2_0=400123456
If>{Length(%TEST_ARRAY_2_0%) = 9}
    Let>MSN=0
    ConCat>MSN,TEST_ARRAY_2_0
    MessageModal>New MSN is %MSN%
ELSE
    If>{Length(%TEST_ARRAY_2_0%) = 10}
        Let>MSN=TEST_ARRAY_2_0
    ELSE
        //MSN not found in .csv - exception?
    EndIf>
EndIf>
Yes, we have a Custom Scripting Service. Message me or go here

DreamTheater
Newbie
Posts: 19
Joined: Mon Oct 14, 2019 6:23 am

Re: Complex Expression Length() issue?

Post by DreamTheater » Thu Dec 19, 2019 1:29 am

Hi Dorian,

Thanks for your reply, I believe the Complex Expression is working (without an error message), but not as expected and ConCat is working as expected.

Essentially my goal with this is to confirm that the value in the array is either 9 or 10 digits.
The MSN variable (Mobile Service Number) is what it'll be 'renamed' to for later use.
FYI all Australian mobile numbers locally are 10 digits starting with 04.

Because of Excel formatting it'll usually drop the leading 0 from numbers causing it to become 9 digits starting with a 4 which is what I have in my TEST_ARRAY_2_0 variable currently.

One of the later steps in my process is to write the MSN into a system which will only accept the full 10 digit number starting with 04, so it's important to have the leading 0 added.

I hope I've explained the purpose well, I've included both the issue and workaround below, try step through them separately and watch the MSN variable.

In the Complex Expression example the results should be:
Start with TEST_ARRAY_2_0=400123456, result is 0400123456 (Expected and working)
Start with TEST_ARRAY_2_0=0400123456, result is 00400123456 (Incorrect result from Length() causing the wrong IF path to be followed adding a leading 0 instead of just letting MSN = the Arr value)

In the Length> example the results should be:
Start with TEST_ARRAY_2_0=400123456, result is 0400123456
Start with TEST_ARRAY_2_0=0400123456, result is also 0400123456 (Due to correct IF path being followed this time)

It might be worth noting that in the errorneous example, changing TEST_ARRAY_2_0=4001234567 does count it correctly as 10 digits, it's only an error when there's a leading 0.

Code: Select all

Let>TEST_ARRAY_2_0=0400123456

If>{Length(%TEST_ARRAY_2_0%) = 9}
    Let>MSN=0
    ConCat>MSN,TEST_ARRAY_2_0
    MessageModal>New MSN is %MSN%
ELSE
    If>{Length(%TEST_ARRAY_2_0%) = 10}
        Let>MSN=TEST_ARRAY_2_0
    ELSE
        //MSN not found in .csv - exception?
    EndIf>
EndIf>





Let>TEST_ARRAY_2_0=0400123456

Length>TEST_ARRAY_2_0,TempLen
If>TempLen=9
    Let>MSN=0
    ConCat>MSN,TEST_ARRAY_2_0
ELSE
    If>TempLen=10
        Let>MSN=TEST_ARRAY_2_0
    ELSE
        //MSN not found in .csv - exception?
    EndIf>
EndIf>

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1380
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Complex Expression Length() issue?

Post by Dorian (MJT support) » Thu Dec 19, 2019 2:00 pm

Aah, thank you for clarifying that, yes, I see exactly what you mean now. I'll mention this to Marcus.
Yes, we have a Custom Scripting Service. Message me or go here

User avatar
JRL
Automation Wizard
Posts: 3526
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Complex Expression Length() issue?

Post by JRL » Thu Dec 19, 2019 2:12 pm

Sorry complex expressions aren't working for you.
Aside from that I would use the Format> function for this task.

Code: Select all

Format>%.10d,TEST_ARRAY_2_0,MSN

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1380
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Complex Expression Length() issue?

Post by Dorian (MJT support) » Thu Dec 19, 2019 2:41 pm

JRL wrote:
Thu Dec 19, 2019 2:12 pm
Sorry complex expressions aren't working for you.
Aside from that I would use the Format> function for this task.

Code: Select all

Format>%.10d,TEST_ARRAY_2_0,MSN
Aah, That's exactly what I just came back here to post. Thank you, Dick. :)
Yes, we have a Custom Scripting Service. Message me or go here

DreamTheater
Newbie
Posts: 19
Joined: Mon Oct 14, 2019 6:23 am

Re: Complex Expression Length() issue?

Post by DreamTheater » Fri Dec 20, 2019 12:00 am

Thanks guys,

I haven't used Format> much so I'll have a play around with that.

Happy holidays!

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1380
Joined: Sun Nov 03, 2002 3:19 am
Contact:

Re: Complex Expression Length() issue?

Post by Dorian (MJT support) » Fri Dec 20, 2019 10:52 am

There are some useful usage examples here.
Yes, we have a Custom Scripting Service. Message me or go here

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Complex Expression Length() issue?

Post by Marcus Tettmar » Sat Dec 21, 2019 1:41 pm

Your value is a number, not a string. It is 6 digits long, not 7. Looks like you want to treat it as a string not a number. So tell it it's a string not a number.

Either set is a string in the first place:

​Let>TEST_ARRAY_2_0={"0400123456"}
If>{Length(%TEST_ARRAY_2_0%) = 10}

​Or (if that isn't possible) refer to it as a string in the comparison:

​Let>TEST_ARRAY_2_0=0400123456
If>{Length("%TEST_ARRAY_2_0%") = 10}
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

DreamTheater
Newbie
Posts: 19
Joined: Mon Oct 14, 2019 6:23 am

Re: Complex Expression Length() issue?

Post by DreamTheater » Sun Jan 05, 2020 11:48 pm

I don't think it's possible to set as a string in the first instance as it's using CSVFileToArray to grab the data.

Referring to it as a string ("%Var%") and the complex expression does work as intended, cheers.

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts