![]() |
Dealing with variables and indices |
Post Reply
|
| Author | |
Tahir
Major Contributor
Joined: 01.Mar.2012 Points: 27 |
Post Options
Thanks(0)
Quote Reply
Topic: Dealing with variables and indicesPosted: 03.Apr.2012 at 19:53 |
|
Hello!
I was wondering if it is possible to 1. Define a variable in the form an array of strings and use indices to get values. 2. For a variable appended with one or more object ids with space separation write a generator function which can pop out the oids one by one. /Tahir |
|
![]() |
|
stevek
MetaCase
Joined: 11.Mar.2008 Points: 643 |
Post Options
Thanks(0)
Quote Reply
Posted: 04.Apr.2012 at 00:03 |
|
1. Sure. For instance to make variables myObjectNames[0], myObjectNames[1] etc.:
foreach .MyObject { variable 'myObjectNames[' $ix++ ']' write id close } But since these are associative variables, you can go further and use anything as the index, not just integers. This lets you build the equivalent of hashed collections: foreach .MyObject { variable 'myObjectNames[' oid ']' write id close } You don't need to enclose the index in square brackets, anything will do, e.g. myObjectNames_0 2. Sure again! You just translate the spaces into newlines, then iterate over the result. E.g. to list the oids to '%spaceToNewline \ \ ' endto /* i.e. escaped space, space (=translates to), escaped newline */ dowhile $spaceSeparatedOids%spaceToNewline { oid ', ' } Edited by stevek - 04.Apr.2012 at 00:06 |
|
![]() |
|
Tahir
Major Contributor
Joined: 01.Mar.2012 Points: 27 |
Post Options
Thanks(0)
Quote Reply
Posted: 04.Apr.2012 at 21:13 |
|
Thanks for the help. It worked to some extent. Now I want to know how can we use other variables as the index and apply it in a conditional statement.
A small code is given below for an illustration of what I am aiming for. $Var2='0' $Var3 ... if $Var1[$Var2] = 'bravo' then 'Print 1' else if $Var1[$Var3] = 'alpha' then 'Print 2' endif endif |
|
![]() |
|
stevek
MetaCase
Joined: 11.Mar.2008 Points: 643 |
Post Options
Thanks(0)
Quote Reply
Posted: 04.Apr.2012 at 23:24 |
|
The IF condition can only contain atomic commands, but you can retrieve and store the array element into a temporary variable and use that in the condition:
variable 'tmp' write variable 'Var1[' $Var2 ']' read close if $tmp = 'bravo' ... In 5.0 you can define _put() and _get() generators as functions to make this neater: _put(@arrayName, @arrayIndex, @elementValue) variable @arrayName '[' @arrayIndex ']' write @elementValue close _get(@arrayName, @arrayIndex) variable @arrayName '[' @arrayIndex ']' read Then you can use them like this: $Var2='0' _put('Var1', $Var2, 'bravo') if _get('Var1', $Var2) = 'bravo' ...
Edited by stevek - 04.Apr.2012 at 23:25 |
|
![]() |
|
dionblain
Member
Joined: 13.Apr.2012 Location: Richmond, VA Points: 1 |
Post Options
Thanks(0)
Quote Reply
Posted: 13.Apr.2012 at 08:18 |
|
Thanks for the help!
|
|
![]() |
|
Post Reply
|
|
| Tweet |
| Forum Jump | Forum Permissions ![]() You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |