Scope of a variable

85 views
Skip to first unread message

Gouraud Jonas

unread,
Jul 7, 2016, 9:47:17 AM7/7/16
to E-Prime
Hi everyone,

Firstly, I assure you that I tried looking at the E-Prime help and previous posts here, but I didn't find what I wanted.

That said, my problem is about the scope of a variable. Here is a simplified version of my experiment :

Dim Questionnaire As Integer = Random(15, 45)
TrialList
     Display the stimulus
     If Questionnaire = the number of trials then
          Ask a question
          Questionnaire = Random(15, 45)
     End if
End when 100 trials are done


Basically I want to display a questionnaire sometimes during the experiment but with random and changing number of trials between each questionnaire, to stop the subject from learning any rythm.
My problem is that the program says that he doesn't know "Questionnaire" within the if. So he cannot use it as a condition.
I tried to define it as a "global" or "public" variable, but then it forces me to define "Questionnaire" as a constant, which is absolutely not what I want.

Thank you for your time, and if you have any suggestion just let me know =)
Pyxel

David McFarlane

unread,
Jul 7, 2016, 2:22:38 PM7/7/16
to e-p...@googlegroups.com
Pyxel,

I see a bit of confusion here. You can supply a value for Const
declarations, but not for Dim declarations; also, you cannot execute any
instructions in the global User area.

So, the following will work at any scope:

Const Questionnaire as Integer = 5

The following will fail at any scope with the error "Expression is not a
constant":

Const Questionnaire as Integer = Random(15, 45)

The following will also fail at any scope, because you cannot initialize
variables within a Dim statement:

Dim Questionnaire as Integer = 5

Instead, you need to simply declare your variable in the global User
area, then give it a value later using inline code (as explained in the
"Using E-Basic" chapter of the User's Guide). So in the global User
area, put

Dim g_Questionnaire as Integer

(I added "g_" to the name to indicate that it is a global variable),
then later in inline code give it a value, e.g.,

g_Questionnaire = Random(15, 45)

I hope that helps.

---------------
David McFarlane
E-Prime training online:
http://psychology.msu.edu/Workshops_Courses/eprime.aspx
Twitter: @EPrimeMaster (https://twitter.com/EPrimeMaster)


2016-07-07 9:47 AM, Gouraud Jonas wrote:
> Firstly, I assure you that I tried looking at the E-Prime help and previous
> posts here, but I didn't find what I wanted.
>
> That said, my problem is about the scope of a variable. Here is a
> simplified version of my experiment :
>
> Dim *Questionnaire *As Integer = Random(15, 45)
> TrialList
> Display the stimulus
> If *Questionnaire *= the number of trials then
> Ask a question
> *Questionnaire *= Random(15, 45)

Gouraud Jonas

unread,
Jul 8, 2016, 5:24:50 AM7/8/16
to E-Prime
Thank you for your answer, indeed it is working now!
I saw that the scope of a variable was limited to the procedure it is declared in. But I thought that it could be seen by other procedures inside the first one. Apparently not.

Thank you again and have a great day =)
Pyxel

Gouraud Jonas

unread,
Aug 12, 2016, 4:18:59 AM8/12/16
to E-Prime
I update this post because the problem is here again.

I put the variable I needed (DurationFixation, used for each trial to last a random amount of time) in the global user area. Then I wanted to update it at the beginning of each trial with the Random function: I used an inline object with a line like this one :
"DurationFixation = Random(1700, 2300)"

Unfortunately it doesn't work very well. Even if DurationFixation is known by all, the slide that uses DurationFixation as a duration only recognize its first value. Put differently, I cannot update the variable for each trial, it's fixed from the first initialization.

Can anyone help me? Thank you all =)
Pyxel

David McFarlane

unread,
Aug 12, 2016, 10:43:40 AM8/12/16
to e-p...@googlegroups.com
Pyxel,

A few thoughts ...

1) Pare your program down to the simplest form that exhibits this
problem. Simplifying and removing all the irrelevant complications will
help you to understand the problem.

2) For debugging purposes, instead of assigning a random value try
simply incrementing the value:

DurationFixation = DurationFixation + 1

3) Now that I look at this again, I don't see why you need a global
variable at all. On each trial you simply assign a random value to your
variable, use that value, and then discard that value before the next
trial. You should be able to do that all within the appropriate
Procedure and keep you variables local. I often find that I can solve
thorny programming problems by rethinking my underlying program
structure, and once I restructure the program on better principles I
solve the original problem and avoid other problems that would have
arisen later.

4) To give a value to an object property, I would almost always use an
attribute reference instead of a variable (see
https://groups.google.com/d/topic/e-prime/RBwU6WDdlG8 ). Attribute
values percolate through the program in a different manner from
variables, which may or may not help in your case. So I would use
something like

c.SetAttrib "DurationFixation", Random(1700, 2300)

And then in the Duration property of Fixation put

[DurationFixation]

---------------
David McFarlane
E-Prime training online:
http://psychology.msu.edu/Workshops_Courses/eprime.aspx
Twitter: @EPrimeMaster (https://twitter.com/EPrimeMaster)


Reply all
Reply to author
Forward
0 new messages