MetaCase Homepage
Forum Home Forum Home > > MetaEdit+
  New Posts New Posts RSS Feed - Dealing with variables and indices
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

Dealing with variables and indices

 Post Reply Post Reply
Author
Message
Tahir View Drop Down
Major Contributor
Major Contributor


Joined: 01.Mar.2012
Points: 27
Post Options Post Options   Thanks (0) Thanks(0)   Quote Tahir Quote  Post ReplyReply Direct Link To This Post Topic: Dealing with variables and indices
    Posted: 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
Back to Top
stevek View Drop Down
MetaCase
MetaCase
Avatar

Joined: 11.Mar.2008
Points: 643
Post Options Post Options   Thanks (0) Thanks(0)   Quote stevek Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
Tahir View Drop Down
Major Contributor
Major Contributor


Joined: 01.Mar.2012
Points: 27
Post Options Post Options   Thanks (0) Thanks(0)   Quote Tahir Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
stevek View Drop Down
MetaCase
MetaCase
Avatar

Joined: 11.Mar.2008
Points: 643
Post Options Post Options   Thanks (0) Thanks(0)   Quote stevek Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
dionblain View Drop Down
Member
Member
Avatar

Joined: 13.Apr.2012
Location: Richmond, VA
Points: 1
Post Options Post Options   Thanks (0) Thanks(0)   Quote dionblain Quote  Post ReplyReply Direct Link To This Post Posted: 13.Apr.2012 at 08:18
Thanks for the help!
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.05
Copyright ©2001-2022 Web Wiz Ltd.

This page was generated in 0.055 seconds.