How make a variable read as integer?

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
dbish
Junior Coder
Posts: 49
Joined: Wed Jan 08, 2003 8:38 am

How make a variable read as integer?

Post by dbish » Thu Apr 01, 2004 7:24 am

I have a problem with variables. I am using Msched 7.2.05 on a Windows XP Home machine.

My script grabs a number from Excel via the clipboard (a number in Excel) and then process that number of loops controlling another program. Script worked fine for small numbers but bombs if trying to do more that 9 loops. Problem seems to be that my counter which tracks how many times I have been through the loop gets one bigger than the first digit of the MAX LOOPS counter (LINECOUNT) and stops processing.

Example - When running a case with 33 loops it stops when i gets to 4. When running a case with 23 loops it stops when i gets to 3. It seems to be doing an alphabetic greater than test and only looking at the first character rather than entire number. Or, for some reason, it is only reading the first number of a 2 digit number. Script snippet below:

GetClipboard>LINECOUNT
Remark> === LOOP to capture each Security data =========
Let>i=1

Label>STARTLOOP
..
..
..
Let>i=i+1
If>i>LINECOUNT,CONTINUE
Goto>STARTLOOP

Label>CONTINUE
..
..
..

I have played with Variable_Explicit and all combinations of %i% vs i, etc. I put a MessageModal line after the continue line to see when it was bombing out:
MessageModal>LOOP STOPPED %i% %LINECOUNT%
which is what keyed me to the action described above.

Anybody have any ideas on integer vs alphabetic variables or any other thoughts??

Thanks

Dave

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Thu Apr 01, 2004 12:56 pm

Dave,

Your code is faultless and runs successfully as expected. I test the script and assign it with a hotkey as follows:

GetClipboard>LINECOUNT
Remark> === LOOP to capture each Security data =========
Let>i=1
Label>STARTLOOP
Let>i=i+1
Msg>i
If>i>LINECOUNT,CONTINUE
Goto>STARTLOOP
Label>CONTINUE

Then, I type any integer value on a text editor, select it with cursor, copy it with ctrl+C. Then press the hotkey. The script stops exactly after n+1 loops, where n is the number I type on the text editor.

The trick here is to truncate all preceding and tailing spaces in your LINECOUNT.

Happy scripting.

dbish
Junior Coder
Posts: 49
Joined: Wed Jan 08, 2003 8:38 am

Works on copied data from Notepad - Not EXCEL!

Post by dbish » Thu Apr 01, 2004 2:51 pm

Thanks for the reply.

I tried your approach - generated LINECOUNT from Notepad - works fine. I then tried creating LINECOUNT from copying from Excel. In Excel the number looks fine - you can do integer operations on the cell value (+A1+1 generates a new integer result as per Excel). I then CTRL-C that into my LINECOUNT variable and I get my old error of the script ending on i=4 when LINCECOUNT=33.

I know this is a little off topic for MSCRIPT forum but does anybody have ideas on why or how to fix??

Thanks

Dave

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Thu Apr 01, 2004 3:28 pm

Dave,

Actually I've already duplicated your problem when preparing my last post. As mentioned there, the trick is to remove the preceding and trailing spaces. Referring to your sample, LINECOUNT=33 may contain (invisible) trailing spaces extracted from your Excel cell.

Please keep posting your problem here.

Happy scripting.

dbish
Junior Coder
Posts: 49
Joined: Wed Jan 08, 2003 8:38 am

Problem solved

Post by dbish » Thu Apr 01, 2004 5:32 pm

I am not quite sure why Excel generates a number that looks like text - no trailing spaces that I could find (I tried ROUND(x,0)), etc. to clean up without luck.

I modified my script to include the following processing which has made it all work.

GetClipboard>LINECOUNT
Len>LINECOUNT,LINECOUNTLEN
If>LINECOUNTLEN>1,BIGLINE
Label>SMALLLINE
Let>LINECOUNTGOOD=LINECOUNT
Goto>BUILDDONE
Label>BIGLINE
MidStr>LINECOUNT,1,1,TENPOS
MidStr>LINECOUNT,2,1,ONEPOS
Let>TENPOS10=TENPOS*10
Let>LINECOUNTGOOD=TENPOS10+ONEPOS
Label>BUILDDONE
..
..
If>i>LINECOUNTGOOD,CONTINUE

Thanks for your help. I spent many hours in the wee of the morning rubbing my eyes and trying to figure out what was wrong with the script. No wonder I couldn't find it.

Thanks agian.

Dave

User avatar
Bob Hansen
Automation Wizard
Posts: 2475
Joined: Tue Sep 24, 2002 3:47 am
Location: Salem, New Hampshire, US
Contact:

Post by Bob Hansen » Fri Apr 02, 2004 1:11 am

You were able to fix your symptoms with some programming, but probablly not necessary, dbish..

armsys was pointing out a well known issue about the Macro Scheduler scripts themselves. I don't know what version of Macro Scheduler you are using, current version is 7.2.50. This version has two menu choices you should understand:
Version 7.2.031 added Remove Trailing Spaces.
Version 7.1.13 added Show All Chars.

In the Editor, select Edit, Show All Chars. This will allow you to see trailing spaces in your code. They are particularly troublesome with numbers and variables, and labels. It is good to be able to see them.

In the Editor, select Edit, Remove Trailing Spaces. This will allow you to easily remove any Trailing Spaces.

I keep Show All Chars on as default. And I have formed the habit of doing Remove Trailing Spaces before I do any Save/Close.
Hope this was helpful..................good luck,
Bob
A humble man and PROUD of it!

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Fri Apr 02, 2004 1:49 am

dbish,

I'm glad that you have succeeded in solving your script enigma. Perhaps you could have saved your time by posting your technical issues here earlier. Many gurus here have been using Macro Scheduler for years.

When walking through your script again, I dscover 2 bytes are appended to any integer (which becomes character datatype in Macro Scheduler) copied by Clipboard from any Excel cell. So, I modify your script as follows:


GetClipboard>LINECOUNT
Msg>LINECOUNT
Len>LINECOUNT,L
Let>Len=L-2
Mid>LINECOUNT,1,len,LINECOUNT

Len>LINECOUNT,len
Remark> === LOOP to capture each Security data =========
Let>i=1
Label>STARTLOOP
Add>i,1
Msg>i
If>i>LINECOUNT,CONTINUE
Goto>STARTLOOP
Label>CONTINUE


I tested and debugged the above script with all kinds of integers, small and large, I entered in Excel. Hope you find the above script useful.

Happy scripting.

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Fri Apr 02, 2004 1:59 am

My Respectable Guru Bob,

How are you? Please forgive me to be of differnet view. In this case, the issue has nothing to do with dbish's script in general and trailing space(s) in particular. When copying with Clipboard from Excel, each Excel cell appears to embed 2 bytes. For example, 123 is of length 5, not 3. I theorize these 2 bytes might contain some datatype and/or formatting and/or unicode information.

Both functions/commands in VBScript and Macro Scheduler can prove the integers copied through Windows' Clipboard is of char datatype, not numeric, in Macro Schulder script.

Happy scripting.

dbish
Junior Coder
Posts: 49
Joined: Wed Jan 08, 2003 8:38 am

Post by dbish » Fri Apr 02, 2004 3:29 am

Dear armsys,

Thanks for all your help.

I agree that your latest approach is more elgant - I copied it out to a help file I am building. It is nice to have my script working - I am constantly amazed how some creative thought and MSched can turn a very long repetitive task into a Push Button / Walk Away / Return in 3 minutes and have an hour of work done.

Thanks again

Dave

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Fri Apr 02, 2004 1:52 pm

Dear dbish,

You're welcome. Have a nice weekend.
Happy scripting.

Lumumba

Post by Lumumba » Fri Apr 02, 2004 10:42 pm

To get rid of leading/trailing spaces I've divided a numeric value with 1 8)

Let>var=var/1

Lumumba

Post by Lumumba » Fri Apr 02, 2004 10:45 pm

Well, additionaly I've used it to eliminate leading "0"

Let>CustID=00000223344
Let>CustID=%CustID%/1
MessageModal>%CustID%

armsys
Automation Wizard
Posts: 1108
Joined: Wed Dec 04, 2002 10:28 am
Location: Hong Kong

Post by armsys » Sat Apr 03, 2004 12:03 am

Lumumba,

During testing, I did try out your method amongst others, that is, Let>var=var/1, but encounter a fatal error.

This is a very special case. bdisk did an excellent job in describing his techncial issue by providng sufficient and necessary information. The clipboard copy is done an Excel cell, not a plain text by itself. For example, what you see is 123 in an Excel cell. After pressing Ctrl+C and copying into clipboard, it becomes "123 ", two extra bytes appended.

Nonetheless, for a pure integer text string, your conversion technicque is creative and brilliant.

Happy scripting.

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