Hey there,
I am writing a procedure that needs to compile the global input into a structure to work further with it.
However, I encounter different errors depending on the approach I try. The main issue I have is that if I define a local variable with the name of the structure, the later definition as a structure is an invalid structure redefinition. If I don't define it as a local variable, I cannot use the pvPack
and pvUnpack
command since the respective variables weren't initialized.
Please see the example below.
proc (1) = example(data);
local out, subgroups, data_1, data_2;
struct PV subgroups;
struct PV out;
subgroups = pvPack(subgroups, data, "start");
i = 1;
do while i <= rows(subgroups.names);
data_1 = pvUnpack(subgroups, subgroups.names[i]);
...
out = pvPack(out,data_1,subgroups.names[i]);
endo;
retp(out);
endp;
Is there a way to use the pvPack
functions within a procedure? Can I somehow call the function without naming the variable before? Or is there another way to add and call elements of a structure within a procedure?
Thanks for any help!
2 Answers
0
accepted
Structures that are declared inside a procedure are local to that procedure. Only weakly typed variables, such as matrices, strings and arrays are declared using the local
keyword.
Take a look at this example:
new;
// This is a global structure
struct pv p;
p = pvPack(pvCreate(), 99, "a");
print "The value from GLOBAL 'p' starts =" pvUnpack(p, "a");
// Call proc which sets local 'p'
pvExample(27);
print "The value from GLOBAL 'p' still =" pvUnpack(p, "a");
proc (0) = pvExample(x);
// This 'p' is local
struct pv p;
p = pvPack(pvCreate(), x, "a");
print "The value from LOCAL 'p' = " pvUnpack(p, "a");
endp;
It will return
The value from GLOBAL 'p' starts = 99.000 The value from LOCAL 'p' = 27.000 The value from GLOBAL 'p' still = 99.000
0
Thanks for your quick support!
The pvCreate() in the first call of pvPack did the job (Y)
Your Answer
2 Answers
Structures that are declared inside a procedure are local to that procedure. Only weakly typed variables, such as matrices, strings and arrays are declared using the local
keyword.
Take a look at this example:
new;
// This is a global structure
struct pv p;
p = pvPack(pvCreate(), 99, "a");
print "The value from GLOBAL 'p' starts =" pvUnpack(p, "a");
// Call proc which sets local 'p'
pvExample(27);
print "The value from GLOBAL 'p' still =" pvUnpack(p, "a");
proc (0) = pvExample(x);
// This 'p' is local
struct pv p;
p = pvPack(pvCreate(), x, "a");
print "The value from LOCAL 'p' = " pvUnpack(p, "a");
endp;
It will return
The value from GLOBAL 'p' starts = 99.000 The value from LOCAL 'p' = 27.000 The value from GLOBAL 'p' still = 99.000
Thanks for your quick support!
The pvCreate() in the first call of pvPack did the job (Y)