How to extract element in array

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
nodochau
Pro Scripter
Posts: 131
Joined: Wed Jan 16, 2019 12:59 pm

How to extract element in array

Post by nodochau » Wed Sep 29, 2021 6:11 pm

Hello All,
I want to extract the element in array if I know the name of it. Thanks for your help.
MDL just returns me a text info_1 :(
Here is my simple code:

Code: Select all

ArrayDim>Names,3
Let>Names_1=A
Let>Names_2=B
Let>Names_3=C

ArrayDim>ID,3
Let>ID_1=123
Let>ID_2=456
Let>ID_3=789

ArrayDim>Status,3
Let>Status_1=Pass
Let>Status_2=Pendding
Let>Status_3=Fail

Let>a=0
Let>text=I need to know data
Position>data,text,1,n_pos
If>n_pos>0
  If>a=0
    Let>info=Names
    MDL>info_1
  Endif
  If>a=1
    Let>info=ID
     MDL>info_1
  Endif
  If>a=2
    Let>info=Status
     MDL>info_1
  Endif
Endif

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: How to extract element in array

Post by Grovkillen » Wed Sep 29, 2021 9:47 pm

Code: Select all

ArrayDim>Names,3
Let>Names_1=A
Let>Names_2=B
Let>Names_3=C

ArrayDim>ID,3
Let>ID_1=123
Let>ID_2=456
Let>ID_3=789

ArrayDim>Status,3
Let>Status_1=Pass
Let>Status_2=Pendding
Let>Status_3=Fail

Let>a=0
Let>text=I need to know data
Position>data,text,1,n_pos
If>n_pos>0
  If>a=0
    Let>info=Names
    Let>Temp=%info%_1
    MDL>Temp
  Endif
  If>a=1
    Let>info=ID
    Let>Temp=%info%_1
    MDL>Temp
  Endif
  If>a=2
    Let>info=Status
    Let>Temp=%info%_1
    MDL>Temp
  Endif
Endif
Let>ME=%Script%

Running: 15.0.24
version history

nodochau
Pro Scripter
Posts: 131
Joined: Wed Jan 16, 2019 12:59 pm

Re: How to extract element in array

Post by nodochau » Thu Sep 30, 2021 10:00 am

Thank you for your help.
I tried and I got <array>_1 from MDL.
Did I miss something?

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: How to extract element in array

Post by Grovkillen » Thu Sep 30, 2021 12:00 pm

Ah, yes the "info" variable is assigned an array... so you need to declare it like this:

Code: Select all

ArrayDim>Names,3
Let>Names_1=A
Let>Names_2=B
Let>Names_3=C

ArrayDim>ID,3
Let>ID_1=123
Let>ID_2=456
Let>ID_3=789

ArrayDim>Status,3
Let>Status_1=Pass
Let>Status_2=Pendding
Let>Status_3=Fail

Let>a=0
Let>text=I need to know data
Position>data,text,1,n_pos
If>n_pos>0
  If>a=0
    Let>info={"Names"}
    Let>Temp=%info%_1
    MDL>Temp
  Endif
  If>a=1
    Let>info={"ID"}
    Let>Temp=%info%_1
    MDL>Temp
  Endif
  If>a=2
    Let>info={"Status"}
    Let>Temp=%info%_1
    MDL>Temp
  Endif
Endif
Let>ME=%Script%

Running: 15.0.24
version history

nodochau
Pro Scripter
Posts: 131
Joined: Wed Jan 16, 2019 12:59 pm

Re: How to extract element in array

Post by nodochau » Thu Sep 30, 2021 12:14 pm

Thanks a lot. It worked.

nodochau
Pro Scripter
Posts: 131
Joined: Wed Jan 16, 2019 12:59 pm

Re: How to extract element in array

Post by nodochau » Thu Sep 30, 2021 1:00 pm

I change my code a little bit. And why the repeat loop does not work though??
I create 3 array first and then go to sub program to add values in each array.

Code: Select all

Let>text1=Names
Let>text2=ID
Let>text3=Status

Let>k=0
Repeat>k
  Add>k,1
  Let>text=text%k%
  ArrayDim>%text%,3
  Gosub>text
Until>k=3

SRT>Names
  Let>Names_1=A
  Let>Names_2=B
  Let>Names_3=C
END>Names

SRT>ID
  Let>ID_1=123
  Let>ID_2=456
  Let>ID_3=789
END>ID

SRT>Status
  Let>Status_1=Pass
  Let>Status_2=Pendding
  Let>Status_3=Fail
END>Status

User avatar
Grovkillen
Automation Wizard
Posts: 1009
Joined: Fri Aug 10, 2012 2:38 pm
Location: Bräcke, Sweden
Contact:

Re: How to extract element in array

Post by Grovkillen » Thu Sep 30, 2021 1:20 pm

I never use the ArrayDim command. I rely on either Separate or RegEx to break a string into an array:

Code: Select all

Let>ARRAY_NAMES=names|id|status
Let>ARRAY_VALUES_names=A|B|C
Let>ARRAY_VALUES_id=123|456|789
Let>ARRAY_VALUES_status=Pass|Pending|Fail

Separate>ARRAY_NAMES,|,TEMP_array_names
If>TEMP_array_names_COUNT>0
  Let>k=0
  Repeat>k
    Let>k=k+1
    Let>TEMP_name=TEMP_array_names_%k%
    GoSub>ADD_VALUES_TO_ARRAY,{%TEMP_name%}
  Until>k=TEMP_array_names_COUNT
Endif>

SRT>ADD_VALUES_TO_ARRAY
  Separate>ARRAY_VALUES_%ADD_VALUES_TO_ARRAY_Var_1%,|,TEMP_array_name
  ArrayRename>TEMP_array_name,,ADD_VALUES_TO_ARRAY_Var_1
END>ADD_VALUES_TO_ARRAY


**BREAKPOINT**
Let>ME=%Script%

Running: 15.0.24
version history

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