Accessing Array elements

Technical support and scripting issues

Moderators: Dorian (MJT support), JRL

Post Reply
Heinz57
Junior Coder
Posts: 28
Joined: Wed Jul 22, 2015 6:10 pm
Location: A Berliner just east of Wien

Accessing Array elements

Post by Heinz57 » Fri Jun 30, 2023 9:39 am

Hello,

I have a problem that drives me mad and that is with the syntax of accessing elements of an Array via a variable. Here is the extract where the problem occurs:

1) There is a file, which was previouly written by MS, with these values:
PageRow^070102^ProjectAbc^1^1^15^320^
PageRow^070102^ProjectAbc^1^1^15^320^
PageRow^070102^ProjectAbc^1^1^15^320^
and a couple of more similar rows

2) This is the code that is trying to read the last line:

SRT>msLoadCurrPageNr
ChangeDirectory>s:\MacroScheduler\LogsCurrPageNr\230629\
Let>msFileNameCurrPageNr=S:\MacroScheduler\LogsCurrPageNr\230629\ProjectAbc.txt

CSVFileToArray>msFileNameCurrPageNr,msArrCurrPagePath
Let>k=msArrCurrPagePath_count

// All of these fail
Let>msArrCurrPageRowLast=%msArrCurrPagePath%_%k%_0
Let>msArrCurrPageRowLast=%msArrCurrPagePath%_k
Let>msArrCurrPageRowLast=%msArrCurrPagePath%_%k%
Let>msArrCurrPageRowLast=%msArrCurrPagePath%_%k%_0
Let>msArrCurrPageRowLast=%msArrCurrPagePath%_%15%_0

// The only chance is to supply an explicit Integer for the row number
// which is nonsense as I need the kfrom msArrCurrPagePath_count
Let>msArrCurrPageRowLast=msArrCurrPagePath_15_0
Let>msArrCurrPageRowLast=msArrCurrPagePath_15_0

END>msLoadCurrPageNr

How can I use a varible? The very sparse example in the doc did not help.

Best
Heinz

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

Re: Accessing Array elements

Post by Dorian (MJT support) » Fri Jun 30, 2023 10:03 am

Here you go (tested) :

Code: Select all

Let>msArrCurrPageRowLast=msArrCurrPagePath_%k%_0
Yes, we have a Custom Scripting Service. Message me or go here

Heinz57
Junior Coder
Posts: 28
Joined: Wed Jul 22, 2015 6:10 pm
Location: A Berliner just east of Wien

Re: Accessing Array elements

Post by Heinz57 » Fri Jun 30, 2023 10:09 am

Thank you very much for the extremely quick reply
and shame on me that I did not test this variant.

Thanks and best regards
Heinz

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

Re: Accessing Array elements

Post by Dorian (MJT support) » Fri Jun 30, 2023 10:15 am

You're very welcome. You were almost there, you just missed that one permutation. :D
Yes, we have a Custom Scripting Service. Message me or go here

Heinz57
Junior Coder
Posts: 28
Joined: Wed Jul 22, 2015 6:10 pm
Location: A Berliner just east of Wien

Re: Accessing Array elements

Post by Heinz57 » Sun Jul 02, 2023 4:07 pm

Hello,

in the above example I had already replaced the count variable for accessing the array from something much longer to %k%, which did work with nothing else changed except the short variable name.

I encountered the same problem today at code written a few days ago but not yet used or tested.

This failed (or even worse, delivered nothing):

Code: Select all

Let>msArrCurrPageRowLast=msArrCurrPagePath_%msArrCurrPageSize%_0
After changing this one line into this:

Code: Select all

Let>k=%msArrCurrPageSize%
Let>msArrCurrPageRowLast=msArrCurrPagePath_%k%_0
it worked. I am on the latest version 15.0.24 and AFAIK such problem did not exist in older version but I had not done much with MS in the last 8-9 months.

I am posting this only FYI and of the readers as my problem is solved.

Best regards
Heinz

Heinz57
Junior Coder
Posts: 28
Joined: Wed Jul 22, 2015 6:10 pm
Location: A Berliner just east of Wien

Re: Accessing Array elements

Post by Heinz57 » Mon Jul 03, 2023 7:22 am

Hello,

I just started testing new modules, some of which were taken from older code. And I encountered the same problem with longer variable names used to access Array elements as already described above.

Only in this case I am absolutely sure that they worked long ago when I used an older version of MS (most likey that was 15.0.20 or even older).

Could this come from changes in the latest 15.0.24?

Best
Heinz

hagchr
Automation Wizard
Posts: 328
Joined: Mon Jul 05, 2010 7:53 am
Location: Stockholm, Sweden

Re: Accessing Array elements

Post by hagchr » Mon Jul 03, 2023 8:01 am

Hello,

This works for me:

Code: Select all

CSVFileToArray>C:\Users\Christer\Desktop\msTest.txt,msArrCurrPagePath
Let>msArrCurrPageRowLast=msArrCurrPagePath_%msArrCurrPagePath_COUNT%_0
How do you define:

Code: Select all

%msArrCurrPageSize%

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

Re: Accessing Array elements

Post by Dorian (MJT support) » Mon Jul 03, 2023 9:04 am

I think hagchr is on the right track there.

This works :

Code: Select all

Let>msFileNameCurrPageNr=d:\00000\ProjectAbc.txt
CSVFileToArray>msFileNameCurrPageNr,msArrCurrPagePath

Let>msArrCurrPageSize=3
Let>msArrCurrPageRowLast=msArrCurrPagePath_%msArrCurrPageSize%_0
This doesn't.

Code: Select all

Let>msFileNameCurrPageNr=d:\00000\ProjectAbc.txt
CSVFileToArray>msFileNameCurrPageNr,msArrCurrPagePath

//Let>msArrCurrPageSize=3
Let>msArrCurrPageRowLast=msArrCurrPagePath_%msArrCurrPageSize%_0
Yes, we have a Custom Scripting Service. Message me or go here

Heinz57
Junior Coder
Posts: 28
Joined: Wed Jul 22, 2015 6:10 pm
Location: A Berliner just east of Wien

Re: Accessing Array elements

Post by Heinz57 » Mon Jul 03, 2023 10:45 am

Thank you in advance.
I cannot care about this before tomorrow earliest - and I will then post my real code.

Tx for the moment!

Krep
Junior Coder
Posts: 31
Joined: Thu Nov 12, 2009 3:56 pm

Re: Accessing Array elements

Post by Krep » Mon Sep 11, 2023 7:07 pm

ooops dupe post
Last edited by Krep on Mon Sep 11, 2023 7:11 pm, edited 1 time in total.

Krep
Junior Coder
Posts: 31
Joined: Thu Nov 12, 2009 3:56 pm

Re: Accessing Array elements

Post by Krep » Mon Sep 11, 2023 7:10 pm

Hello! Similar question along these lines... Is it possible to assign or retrieve a value from/to an array element inside a complex expression?

For example:
Let>Stack_1=A
Let>Stack_2=B
Let>Ptr=2
//Let>TempVar=Stack_%Ptr%
//StringReplace>A;B;C;D,%TempVar%;,,holder
StringReplace>A;B;C;D,%Stack_%Ptr%%;,,holder

the desired output is for holder=A;C;D

I know I can simply use another statement by assigning a temporary variable such as uncommenting the two lines in the middle!! But I would rather not because this is a simplified version of my actual code which has many passed variables and is much more complicated; it would be nice to be able to handle it in a single complex line instead of multiple temporary variables... Just checking if there's an easier way...

I've tried variations such as:
StringReplace>A;B;C;D,{%Stack_%Ptr%%};,,holder
StringReplace>A;B;C;D,%{Stack_%Ptr%}%;,,holder
StringReplace>A;B;C;D,{%("Stack_"%Ptr%)%+";"},,holder

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

Re: Accessing Array elements

Post by Dorian (MJT support) » Mon Sep 11, 2023 9:16 pm

Your commented out method is the correct way. You can't use complex expressions with StringReplace.
Yes, we have a Custom Scripting Service. Message me or go here

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