Cannot increment array element into variable

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
obfusc88
Pro Scripter
Posts: 87
Joined: Wed Mar 14, 2007 6:22 pm

Cannot increment array element into variable

Post by obfusc88 » Wed Apr 14, 2021 5:30 pm

//Have an array vMembers with 12 elements.
//Want to loop thru elements 2-11 into vHaystack2 for RegEx work
Cannot reload vHaystack2 with Count variable

Code: Select all

//Set up arrray
ArrayDim>vNames,3
Let>vNames_1=Fred
Let>vNames_2=Sally
Let>vNames_3=John
Let>vThisRecord=1
Add>vThisRecord,1
MDL>This record is %vThisRecord%

//Fill Haystack from array
//This does work, element is hardcoded. Sally is in Haystack2, 
Let>vHaystack2=%vNames_2%
MDL>%vHaystack2%

//This does NOT work, element is variable.vNames_2 is in Haystack2
Let>vHaystack2=vNames_%vThisRecord%
MDL>%vHaystack2%
//How to call next array using vThisRecord that is incrementing?

Edited: My mistake, this example does work properly. See subsequent post with actual code that fails.
Last edited by obfusc88 on Wed Apr 14, 2021 9:50 pm, edited 1 time in total.

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

Re: Cannot increment array element into variable

Post by JRL » Wed Apr 14, 2021 7:33 pm

Your example works for me. I do exactly this in 90% or more of the scripts I write. I dream this code I've typed it so often

Let kk=0
Repeat>kk
Add>kk,1
Let>value=array_%kk%
Blah..
Blah..
Until>kk=array_count

Maybe post a chunk of your failing code?

Or am I missing something. What do you expect vHaystack2 to display the second time. I expect and get "Sally"

mightycpa
Automation Wizard
Posts: 343
Joined: Mon Jan 12, 2004 4:07 pm
Location: Vienna, VA

Re: Cannot increment array element into variable

Post by mightycpa » Wed Apr 14, 2021 8:07 pm

Me too. Took it verbatim from your post.

Results are: 2, Sally, Sally.

What do you see in the 3rd dialog?
"A facility for quotation covers the absence of original thought." - Lord Peter Wimsey

obfusc88
Pro Scripter
Posts: 87
Joined: Wed Mar 14, 2007 6:22 pm

Re: Cannot increment array element into variable

Post by obfusc88 » Wed Apr 14, 2021 9:07 pm

Looks like my example was done too fast, it works for me also.
So there must be an error in my actual code.
Here is from my code:

Code: Select all

Label>LOOP1

**BREAKPOINT**
//Next line is earlier in code outside loop to prime the first element.  Included here for testing.
Let>vThisRecord=1

//Have an array vMembers with 12 elements.
//Want to loop thru elements 2-11 into vHaystack2 for RegEx work
//Cannot reload vHaystack2 with variable?
//Added line to measure length for quick validation.  vMembers_2 is 2350 chars

//Loop while vThisRecord is less than %vTotalRecords
//Let>vHaystack2=""
Add>vThisRecord,1

//Read next array member for parsing
Let>vHaystack2=vMembers_%vThisRecord%
Length>%vHaystack2%,vHay2Len
//Line above does not work with variable.  

//Next line works OK for testing RegEx on one array element.
//Need to increment with each loop cycle using vThisRecord
Let>vHaystack2=%vmembers_2%
Length>%vHaystack2%,vHay2Len
*/

mightycpa
Automation Wizard
Posts: 343
Joined: Mon Jan 12, 2004 4:07 pm
Location: Vienna, VA

Re: Cannot increment array element into variable

Post by mightycpa » Wed Apr 14, 2021 10:05 pm

So far, you're missing a DIM, which would make vMembers_%vThisRecord% not an array element.

Does vMembers look like an array where you can inspect variables?
"A facility for quotation covers the absence of original thought." - Lord Peter Wimsey

obfusc88
Pro Scripter
Posts: 87
Joined: Wed Mar 14, 2007 6:22 pm

Re: Cannot increment array element into variable

Post by obfusc88 » Wed Apr 14, 2021 11:25 pm

I mentioned that I have an array, vMembers with 12 elements. Each element is about 2000-3000 characcters. I did a RegEx to enter tilde characters (~), and then used Separate command on the tilde to break the large document into smaller manageable elements. The array exists, the elements exist. The Array count=12, and is not a problem.
My loop follows std loop routines, have done thousands. My loop counter is named vThisRecord.
But my loop counter value is not being accepted as a value to update the variable vHaystack2. Hardcoded values work fine.
If I do this:
Let>vHaystack2=%vMembers_2%,
it works perfectly. The value of vHaystack2 can be reviewed in the Watch list, and is 2350 characters long.

But, if I do this:
Let>vHaystack2=vMembers_%vThisRecord%, then the value of vHaystack2 becomes the text value,"vMembers_2", it does not become the value of the element vMembers_2.

If I do this, vHaystack2 updates with each line.
Let>vHaystack2=%vMembers_2%
Let>vHaystack2=%vMembers_3%
Let>vHaystack2=%vMembers_4%
Let>vHaystack2=%vMembers_5%

Is that more clear to understand? Thanks for listening.

mightycpa
Automation Wizard
Posts: 343
Joined: Mon Jan 12, 2004 4:07 pm
Location: Vienna, VA

Re: Cannot increment array element into variable

Post by mightycpa » Wed Apr 14, 2021 11:59 pm

Hi,

Yeah, I get what you're saying, I only mentioned that because what we think we're doing and what we're actually doing are different things all too often. But if you can see an array, then that's obviously not it.

I think it's helpful to have the actual code you're having issues with, although sometimes I realize that's not something you want to share.

So the only other thing that jumps to mind is the VarExplicit setting. In the very first example you provided, you can get the mysterious behavior you describe by adding Let>VarExplicit=1. Change it to 0 and it works right.
"A facility for quotation covers the absence of original thought." - Lord Peter Wimsey

obfusc88
Pro Scripter
Posts: 87
Joined: Wed Mar 14, 2007 6:22 pm

Re: Cannot increment array element into variable

Post by obfusc88 » Thu Apr 15, 2021 1:13 am

YES! That was the fix. Problem Solved!
I changed VarExplicit from 1 to 0 and it works right.

Thanks for the fix. Stay tuned for more problems.......

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