Page 1 of 1

Info on an array

Posted: Thu May 06, 2004 6:35 pm
by Furor
Can you still use an array that holds different vars?

IE holds an int in one part and holds a string in another.

Thanks
Furor

Posted: Thu May 06, 2004 7:24 pm
by Lax
Wait.. the answer to your second question is no.. but.. the answer to your first question is yes.

Arrays can only be made to hold one type. Previously, there were no "types", everything was a string. Now, there are types. Not everything is a string. So, one way to do it is to make your array hold strings, and it can magically hold ints in the form of strings.. e.g. the int 1234 is stored inthe array as a string containing "1234" .. like, wow! The other way is to use an "array of pointers"... pointer isnt really the term, but let me explain.

At the bottom of the MQ2DataVars reference is a section titled "Tricksy Hobbitses" something or other. It explains how a string can store the name of a variable, and then referenced later.

For example.

Code: Select all

/declare mystring string local My String
/declare mypointer string local mystring
/echo ${${mypointer}}
The echoed value is "My String". What happens is the inside ${} gets parsed, making "${mystring}" and then the outside ${} gets parsed, making "My String", the value of mystring.

This applies equally to arrays:

Code: Select all

/declare mystring1 string local My String A
/declare mystring2 string local My String B
/declare myarray[2] string local
/varset myarray[1] mystring1
/varset myarray[2] mystring2
/echo ${${myarray[1]}}
/echo ${${myarray[2]}}
The output is:
My String A
My String B


Hope this helps.

Posted: Thu May 06, 2004 10:03 pm
by Furor
Thank you Lax as always great work and great help

Furor