viewpoint-particle

Author Topic: ItemExtract  (Read 11862 times)

wilson1001

  • Newbie
  • *
  • Posts: 6
ItemExtract
« on: August 20, 2013, 02:28:48 am »
I just had a quick question regarding ItemExtract.  I have a line of code that extracts a string variable i called numbpage (number of pages).  I subsequently use numbpage in a For/Next Loop as an Integer.  The For/Next Loop runs fine but at the end of the loop, it has error 3361 msg (implying Numbpage is a string):

"For error:  For counters must be numbers, not strings"

I've tried to convert it integer by using "n=Int(Numbpage)" and "n=Int("Numbpage")" but that does not work.  Confusing that it runs through the For loop fine (implying Numbpage is an integer), but then breaks on the error at the end (implying Numbpage is a string), thus not allowing my program to continue beyond the For loop.  I've also output onto notepad each increment of value of P and watched it count up from 1 to 10 (i used 10 as test for NumbPage value) but the error pops up after completing the loop after integer 10.


NumbPage=ItemExtract(2,line,"~")


For P = 1 to NumbPage

Next

kdmoyers

  • Sr. Member
  • ****
  • Posts: 339
Re: ItemExtract
« Reply #1 on: August 20, 2013, 04:35:00 am »
Interesting. 
Any chance you could snip out a little short section of code that demonstrates the problem, and then post it here?
-Kirby
The mind is everything; What you think, you become.

Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: ItemExtract
« Reply #2 on: August 20, 2013, 08:05:58 am »
WinBatch auto magically converts a variable type based on it use. The ItemExtract function returns a string. The For loop treats the variable as an integer. if for some reason the value of your variable cannot be converted to a valid integer, you could get this error in your For loop.

I recommend using IsInt to confirm you have a valid integer  before you execute the For loop.  For debugging the VarType function can be used to watch the NumbPages variable's data type. For example:

Code: Winbatch
;line = "9"  ;forces an error because there is no second value to extract
line = "9~10~11"
NumbPage=ItemExtract(2,line,"~")
; Confirm this can be converted to an integer
If !isInt(NumbPage)
    Pause('Notice', 'NumbPage cannot be converted to a valid interger': @lf: 'The value of numbpage is "': NumbPage :'"')
    exit
Endif
For P = 1 to NumbPage
    Pause('VarType(NumbPage)', VarType(NumbPage))
Next
Exit
Deana F.
Technical Support
Wilson WindowWare Inc.

JTaylor

  • Pundit
  • *****
  • Posts: 1362
    • Data & Stuff Inc.
Re: ItemExtract
« Reply #3 on: August 20, 2013, 08:13:55 am »
Also, if it is a situation where you *know* it will always be a number you can do something like:

    NumbPage=ItemExtract(2,line,"~")+0

and that will solve the problem without additional error checking.

Jim

Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: ItemExtract
« Reply #4 on: August 20, 2013, 09:07:24 am »
Also, if it is a situation where you *know* it will always be a number you can do something like:

    NumbPage=ItemExtract(2,line,"~")+0

and that will solve the problem without additional error checking.

Jim

I suspect this user is getting this error because the value is in fact NOT a number and cannot be converted.
Deana F.
Technical Support
Wilson WindowWare Inc.

JTaylor

  • Pundit
  • *****
  • Posts: 1362
    • Data & Stuff Inc.
Re: ItemExtract
« Reply #5 on: August 20, 2013, 10:17:12 am »
May be.  I obviously don't know but I have had cases where I've encountered this type of problem and it was a number.   Mainly just passing the suggestion on as something that will solve such problems when it is a number and should always be a number but isn't converting properly for some reason.

Jim

Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: ItemExtract
« Reply #6 on: August 20, 2013, 11:01:57 am »
May be.  I obviously don't know but I have had cases where I've encountered this type of problem and it was a number.   Mainly just passing the suggestion on as something that will solve such problems when it is a number and should always be a number but isn't converting properly for some reason.

Jim

If the string contains any string information that cannot be converted( i.e. @LF ) the +0 suggestion will fail. Hence the suggestion to use IsInt to capture this possibility.
Deana F.
Technical Support
Wilson WindowWare Inc.

JTaylor

  • Pundit
  • *****
  • Posts: 1362
    • Data & Stuff Inc.
Re: ItemExtract
« Reply #7 on: August 20, 2013, 11:29:42 am »
I agree...that is why I said in my original post that it should only be used when one *knows* that it will be a number.

Jim

Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: ItemExtract
« Reply #8 on: August 20, 2013, 11:49:25 am »
I agree...that is why I said in my original post that it should only be used when one *knows* that it will be a number.

Jim

Yes but just to clarify...that method should only be used when one *knows* that it will be a number *AND* that it could not possibly have any embedded string information.
Deana F.
Technical Support
Wilson WindowWare Inc.

wilson1001

  • Newbie
  • *
  • Posts: 6
Re: ItemExtract
« Reply #9 on: August 20, 2013, 12:59:29 pm »
Hi KDMoyers.  I want to thank everyone for the help.  I thought it was something simple I am missing.  The snippet of code I have below is what I have so far.  I've tested it by outputting numbpage and P to Notepad and watched it count up to 10.  I just picked a random number to feed into it from a very simple input file that had two entries.  IssueID (which is a string) and 10 separated by delimiter ~.

I tried appending +0 at the end of Numbpage and outputted that to a notepad, and I can add numbers to it as if its an integer (I used a=NumbPage + 43, Sendkey(a), to notepad and it outputted 53).  I guess somewhere along the line, it is getting reconverted back into a string.   I will try the isInt(Numbpage), I was looking for a command that would check it.  whats strange is that it is able to add another random number and output it as an integer, and it is counting up in the For loop also.  thank you again for everyone's comments.

template="template.txt"
infile="inputdata.txt"

; A standard FileOpen to read in the input file, line by line
handle=FileOpen(infile,"READ")

While 1
   line=FileRead(handle)
   If line=="*EOF*" Then Break

   IssueID=ItemExtract(1,line,"~")
   NumbPage=ItemExtract(2,line,"~")

   templatedata=FileGet(template)

   templatedata=StrReplace(templatedata,"{{IssueID}}",IssueID)

   For P = 1 to NumbPage

   k = StrFixLeft(P,"0",4)

      Sendkey(k)
      Sendkey("{enter}")
      TimeDelay(1)
      Sendkey("{enter}")
   Sendkey(Numbpage)
   Sendkey("{enter}")
   Sendkey(P)
   Sendkey("{enter}")

    Next


EndWhile      ; Repeat for each line of the file

Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: ItemExtract
« Reply #10 on: August 20, 2013, 01:43:32 pm »
It is always a string variable that is returned from ItemExtract. The data type NEVER changes. However when it is used in ANY operation that requires an integer ( like a For Loop) then WinBatch treats it as an integer. If you have a scenario where the For loop is erroring complaining about it NOT being an integer you should use IsInt to confirm the string can be converted to a valid integer.

From the help file under the topic 'Variables':
Quote
A variable may contain an integer, a floating point number, a string, a list, an array or a string representing an integer or a floating point number . Automatic conversions between numbers and strings are performed as a matter of course during execution.

If a function requires a string parameter and a numeric parameter is supplied, the variable will be automatically modified to include the representative string.

If a function requires a numeric parameter and a string parameter is supplied, an attempt will be made to convert the string to the required numeric parameter. If it does not convert successfully, an error will result.

Here is some modified code to handle this situation:
Code: Winbatch
template="template.txt"
infile="inputdata.txt"

; A standard FileOpen to read in the input file, line by line
handle=FileOpen(infile,"READ")

While 1
   line=FileRead(handle)
   If line=="*EOF*" Then Break
   
   IssueID=ItemExtract(1,line,"~")
   NumbPage=ItemExtract(2,line,"~")

   ; Confirm this can be converted to an integer
   If !isInt(NumbPage)
       Pause('Notice', 'NumbPage cannot be converted to a valid integer': @lf: 'The value of numbpage is "': NumbPage : '"' : @lf : 'String Length =  ': StrLen(NumbPage))
       exit
   Endif
   
   templatedata=FileGet(template)
   templatedata=StrReplace(templatedata,"{{IssueID}}",IssueID)
   
   For P = 1 to NumbPage
      k = StrFixLeft(P,"0",4)
      Sendkey(k)
      Sendkey("{enter}")
      TimeDelay(1)
      Sendkey("{enter}")
      Sendkey(Numbpage)
      Sendkey("{enter}")
      Sendkey(P)
      Sendkey("{enter}")
   Next
EndWhile      ; Repeat for each line of the file
Deana F.
Technical Support
Wilson WindowWare Inc.

wilson1001

  • Newbie
  • *
  • Posts: 6
Re: ItemExtract
« Reply #11 on: August 20, 2013, 02:23:42 pm »
Hi Deana,

You are right.  The ItemExtract is picking up an integer from my input file and always treating it as a string.  Numbpage remains a string throughout the whole process (I checked with VarType command).  The variable P in the For loop is always an integer and that's why its able to count up the loop.  I also used the IsInt(Numbpage).  surprisingly, it returns a 1 (which I assume to be @true), so it says that it can be converted into an integer but when I use the command Int (e.g.  n=Int("Numbpage"), I get error 3057, Variable could not be converted to a valid number.    Strange that IsInt returns @true.  I guess I'll dig around for a workaround.  If you happen to know an equivalent command to ItemExtract that can read input from a file as an integer, that would be helpful.  Thanks for your responses.

Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: ItemExtract
« Reply #12 on: August 20, 2013, 02:31:43 pm »
Hi Deana,

You are right.  The ItemExtract is picking up an integer from my input file and always treating it as a string.  Numbpage remains a string throughout the whole process (I checked with VarType command).  The variable P in the For loop is always an integer and that's why its able to count up the loop.  I also used the IsInt(Numbpage).  surprisingly, it returns a 1 (which I assume to be @true), so it says that it can be converted into an integer but when I use the command Int (e.g.  n=Int("Numbpage"), I get error 3057, Variable could not be converted to a valid number.    Strange that IsInt returns @true.  I guess I'll dig around for a workaround.  If you happen to know an equivalent command to ItemExtract that can read input from a file as an integer, that would be helpful.  Thanks for your responses.

You are using quotes around the variable name! This treats the data between the quotes as a STRING. Remove the quotes to pass the variable:

Code: Winbatch
n=Int(Numbpage)
Deana F.
Technical Support
Wilson WindowWare Inc.

wilson1001

  • Newbie
  • *
  • Posts: 6
Re: ItemExtract
« Reply #13 on: August 20, 2013, 03:18:57 pm »
Hi Deanna,

I actually tried it both ways, with and without quotes, and it still did was not able convert the string to integer.  sorry, I should have posted both attempts.  Thanks.

JTaylor

  • Pundit
  • *****
  • Posts: 1362
    • Data & Stuff Inc.
Re: ItemExtract
« Reply #14 on: August 20, 2013, 08:49:46 pm »
Can you post your text files so we can try it on our side?   At this point there is really no good way for anyone to help here if we can't see what you are doing.   It sounds like you are hitting a point in your data where you aren't getting a number and it should have shown you what it hit with the code Deana posted.

Jim

wilson1001

  • Newbie
  • *
  • Posts: 6
Re: ItemExtract
« Reply #15 on: August 21, 2013, 08:50:39 am »
Hi Jim,

Thank you for the help.  The text files i am inputting into the script is very simple one line sample text files.  they are set up just to name a particular issue of a magazine (e.g.  Dec 2007) and the number of pages that issue had (e.g.  10 pages).   So template.txt is just a one line text saved in notepad.  I wanted to have the script work before expanding the template and data files.  The template.txt portion of the program works fine as it keeps everything as a string.  but inputting the data is where it stops as the extracted Numbpage variable remains a string and will not convert to integer.

Template.txt (saved to notepad)

The magazine for the month of {{IssueID}} has {{Numbpage}} of pages.

Inputdata.txt (saved to notepad)

Dec2007~10

The idea is to create a small text file that catalogs the magazine with number of pages.  The For loop with numbpages is meant to help me scroll through the pages automatically.  thanks.

Deana

  • Wilson WindowWare Tech Support
  • Pundit
  • *****
  • Posts: 1183
  • WinBatch® can do it.
    • WinBatch Tech Support Database
Re: ItemExtract
« Reply #16 on: August 21, 2013, 09:47:11 am »
Please post the code that your are currently having an issue with. Make sure you added the IsInt/ StrLen code I posted previously to catch the possibility that the variable CANNOT be treated as an integer.
Deana F.
Technical Support
Wilson WindowWare Inc.

JTaylor

  • Pundit
  • *****
  • Posts: 1362
    • Data & Stuff Inc.
Re: ItemExtract
« Reply #17 on: August 21, 2013, 11:27:52 am »
Meant to ask this before...do you have a blank line in your file?  That would cause the problem you are having.

Jim

wilson1001

  • Newbie
  • *
  • Posts: 6
Re: ItemExtract
« Reply #18 on: August 21, 2013, 02:33:12 pm »
Hi Deana,

I added the IsInt/StrLen code you posted and.....it worked!!!  Thank you so much.  Thank you to everyone for the input.


JTaylor

  • Pundit
  • *****
  • Posts: 1362
    • Data & Stuff Inc.
Re: ItemExtract
« Reply #19 on: August 22, 2013, 06:11:18 am »
So what was the problem?

Jim