Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Solved]Does function exists?
#1
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.
#2
iif
action1 and action2 must be expressions returning values of the same type.
#3
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?
#4
iif(condition Function1 Function2)
for goto use sel
#5
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
....
#6
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.
#7
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)
#8
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,...).
#9
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?
#10
Try VARIANT.

function VARIANT'v

sel v.vt
,case VT_I4
,use v.lVal
case VT_BSTR
,use v.bstrVal
...
#11
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 Smile
#12
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.
#13
I usually call by reference when possible.

Thanks for help.
#14
In your case this can be used probably.

Function Function137
Code:
Copy      Help
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
,
#15
Possibly this will be faster. Use perf to measure speed.

Function Function138
Code:
Copy      Help
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
Code:
Copy      Help
Function138 "one[]two"

ARRAY(str) a="three[]four"
Function138 0 a
#16
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".


Forum Jump:


Users browsing this thread: 1 Guest(s)