Posts: 795
Threads: 136
Joined: Feb 2009
Hi,
i want to test if a string is empty and do something upon result.
is there a function like in C++
(empty(s))?action1:action2
or is it only possible to do a sel function?
int i=empty(s)
sel i
case 1
action1
case 2
action2
or another simpler way?
Thanks.
Posts: 11,933
Threads: 138
Joined: Dec 2002
iif
action1 and action2 must be expressions returning values of the same type.
Posts: 795
Threads: 136
Joined: Feb 2009
Gintaras Wrote:iif
action1 and action2 must be expressions returning values of the same type.
yes and no..
In the example, it assign value to a variable.
Does it allow to jump to function, or goto instead?
Posts: 11,933
Threads: 138
Joined: Dec 2002
iif(condition Function1 Function2)
for goto use sel
Posts: 795
Threads: 136
Joined: Feb 2009
Hmm, a derivate from it, but tougher
This does not work.why?
function [str&_Resstr] [ARRAY(str)&_Resarray]
if(&_Resstr)
type str _Coll
else type ARRAY(str) _Coll
foreach _Item _Coll
....
Posts: 11,933
Threads: 138
Joined: Dec 2002
declarations are used only when compiling. Imagine that at run time these statements are removed.
if(&_Resstr)
,;;removed
else ;;removed
and we have empty if/else.
Also, QM does not have variable scopes smaller than function. Scope of other declarations is global.
Posts: 795
Threads: 136
Joined: Feb 2009
Ok, i got it.
So, be function A, accepting either str or array as parameters, each optional
function [str&STR] [ARRAY(str)&ARR]
it test is STR or a line of ARR is empty
1) How to share the same code with different types of parameters
if (@ [ARRAY(str)&ARR]) or ( [str&STR] @)
Coll = ARR or STR
foreach item Coll
2)Any cast conversion?
str(ARR[i]) as
int i=3
str s=str(i)
Posts: 11,933
Threads: 138
Joined: Dec 2002
Same code cannot be shared between variables of unrelated types, such as str and ARRAY. And casting/converting unrelated types is not possible, except in some cases (str=int, str=ARRAY,...).
Posts: 795
Threads: 136
Joined: Feb 2009
Yes, but
str s
s=ARR[0]
s.ddd
s.srrr
ARR[0]=s
so no possibility to test type of variable, and do certain function on certain type?
I must do two functions depending on argument type, and i cant share code between them?
Posts: 11,933
Threads: 138
Joined: Dec 2002
Try VARIANT.
function VARIANT'v
sel v.vt
,case VT_I4
,use v.lVal
case VT_BSTR
,use v.bstrVal
...
Posts: 795
Threads: 136
Joined: Feb 2009
Well, i did a workaround.
Used an array, and use it.
if str with one ligne -> use array[0]
else iterate array.
But i wonder if there is a benefit in terms of speed or memory consumption.
str -> call function1 for str
array -> call funtion2 for array
function1 & function2 do the same thing
array or str -> call function2, i declare an array for only a 1 line string.
Any opinion?
for the variant stuff, i keep it for another macro
Posts: 11,933
Threads: 138
Joined: Dec 2002
Speed and memory depends on how arguments are declared. If by value (TYPE'x), x is copied. If x is string or array, whole string/array data is copied. It is slower. If by reference (TYPE&x), only address of the caller's variable is passed. It is fast. But speed and memory is important only if the function will be called frequently or arguments will be large.
Posts: 795
Threads: 136
Joined: Feb 2009
I usually call by reference when possible.
Thanks for help.
Posts: 11,933
Threads: 138
Joined: Dec 2002
In your case this can be used probably.
Function Function137
function [str&_Resstr] [ARRAY(str)&_Resarray]
ARRAY(str) _a
if(!&_Resarray) _a=_Resstr; &_Resarray=_a ;;convert str to ARRAY and set _Resarray to be reference to _a
int i
for i 0 _Resarray.len
,str& r=_Resarray[i]
,out r
,
Posts: 11,933
Threads: 138
Joined: Dec 2002
Possibly this will be faster. Use perf to measure speed.
Function Function138
function [$_Resstr] [ARRAY(str)&_Resarray]
int i n=iif(&_Resarray _Resarray.len numlines(_Resstr))
for i 0 n
,str& r
,if(&_Resarray) &r=_Resarray[i]; else _s.getl(_Resstr -i); &r=_s
,out r
,
test
Macro Macro1099
Function138 "one[]two"
ARRAY(str) a="three[]four"
Function138 0 a
Posts: 795
Threads: 136
Joined: Feb 2009
Nice, does what desired.
For my purpose, both are quite equal in perf result.
But i prefer the second way, as it seems more "flexible".
|