Changing Global variables inside function

Hello all,
Trying to set a global variable in a function that had already been set before the function. For example:
$VariablesAreSame = $false
function CheckIfDifferent ($variable1, $variable2){
If($variable1 -eq $variable2){
$VariablesAreSame = $true
write-host $VariablesAreSame
for($i=1;$i -le 5;$i++){
CheckIfDifferent $i 2
When this is run, $VariablesAreSame is never permanently set within the function, which I would like it to be. Any ideas?

You need to set $VariablesAreSame as a global variable in the function.
$VariablesAreSame = $false
function CheckIfDifferent ($variable1, $variable2){
If($variable1 -eq $variable2){
$global:VariablesAreSame = $true
write-host $VariablesAreSame
for($i=1;$i -le 5;$i++){
CheckIfDifferent $i 2

Similar Messages

  • [Bash] How to `declare` a global variable inside a function?

    #!/bin/bash
    # Not using declare -A, array will be treated as indexed array rather than an associative array
    seta()
    for ((n=0;n<$1;n++)); do
    printf -v a_$n[k] %s k$n
    printf -v a_$n[j] %s j$n
    eval echo inside seta: a_$n[k] = \$\{a_$n\[k\]\}
    done
    seta 3
    echo outside: a_0[k] = ${a_0[k]}
    echo outside: a_0[j] = ${a_0[j]}
    echo
    # Use declare -A inside function, array will be undefined outside
    setb()
    for ((n=0;n<$1;n++)); do
    declare -A b_$n # Note here
    printf -v b_$n[k] %s k$n
    printf -v b_$n[j] %s j$n
    eval echo inside setb: b_$n[k] = \$\{b_$n\[k\]\}
    done
    setb 3
    echo outside: b_0[k] = ${b_0[k]}
    echo outside: b_0[j] = ${b_0[j]}
    echo
    # The bad solution, only works if we know beforehand how many c_? arrays will be assigned inside setc()
    for ((n=0;n<3;n++)); do
    declare -A c_$n
    done
    setc()
    for ((n=0;n<$1;n++)); do
    printf -v c_$n[k] %s k$n
    printf -v c_$n[j] %s j$n
    eval echo inside setc: c_$n[k] = \$\{c_$n\[k\]\}
    done
    setc 3
    echo outside: c_0[k] = ${c_0[k]}
    echo outside: c_0[j] = ${c_0[j]}
    My original code does the declare -A as in setb(). I was surprised when I saw all blank output... Now the problem is illustrated clearly by the setb() example.
    The setc() example works, but a bit ugly: look at the two 3's...
    My ideal solution would be, something like `declare_global -A b_$n` in setb()
    Any ideas?

    with current bash versions, i don't think it is possible to declare global associative arrays in functions.
    what you are after is the "-g" option of typeset which is available in zsh.
    i think i read somewhere that something similar may be planned for bash 4.2.

  • How to change Global variable in the query

    Hi,
    I have 6 reports which is using 0PDT as a global variable and has single option as a property. Now I want to change this property and have multiple selection but I am not able to change its property.
    I am able to see all the filter variables from the query property and from there I am getting edit option to change but the "single selection" option is grey/disable which is preventing me to make change in this variable. Is there any way that I delete this variable and create a local variable which has date range option.
    This is affecting all my reports and all the report needs date ranges instead of single select option.
    Thanks,
    Shivani

    Shivani,
        You can change the restrictions with new variable(right click on required infoobject and choose restrict -->> choose variables -->> move existing variable from right to left and move  new variable. Then you can see only one variable.
    You can not delete any variable if is using in some queries.
    how to restrict:
    Srini<a href="http://help.sap.com/saphelp_nw04/helpdata/en/f1/0a563fe09411d2acb90000e829fbfe/content.htm">Restricting Characteristics</a>

  • Change global variable from symbol

    Hi Everyone,
    I need help making changes to a global variable.  I set a variable up on the creationComplete using the code: sym.setVariable("information", 1);
    Later in the project I want to call up that variable and then increase the value on a button click. In the button symbol (onClick) I can call up the variable by using: var paragraph = sym.getComposition().getStage().getVariable("information");
    When I use an alert(paragraph), the proper value shows up.  But, on the click, I need to change the value of the stageComplete variable "information" to 2.
    Can anyone give me some guidance on what I'm not doing right?
    Thanks
    Randy

    Hi,
    It seems you want to increment using sym.setVariable() and sym.getVariable().
    First step: sym.setVariable("counter",1); // it means: counter = 1;
    Second step (another panel): sym.setVariable("counter", sym.getVariable("counter") + 1); // it means: counter = counter +1; or counter += 1
    Demo: https://app.box.com/s/dqml4wtbcimtjvlkf2ol

  • Change global variable multithreaded

    Hi,
    I've encounterd an issue which I'm trying to end a thread from another thread
    I have one main thread that calls two different threads (Thread1 & Thread2) as so:
    Thread1()
       while (g_ThLoop)
              ...Do stuff...
       ...Cleanning memory etc....
    Thread2()
       g_ThLoop  = 0;
    MainThread()
        g_ThLoop = 1;
       CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE, Thread1, 0, &hFid1);
       ....Do Stuff...
       CmtScheduleThreadPoolFunction (DEFAULT_THREAD_POOL_HANDLE, Thread2, 0, &hFid2);
    CmtWaitForThreadPoolFunctionCompletion (DEFAULT_THREAD_POOL_HANDLE, hFid1, 0);
    CmtWaitForThreadPoolFunctionCompletion (DEFAULT_THREAD_POOL_HANDLE, hFid2, 0);
    CmtReleaseThreadPoolFunctionID (DEFAULT_THREAD_POOL_HANDLE, hFid1);
    CmtReleaseThreadPoolFunctionID (DEFAULT_THREAD_POOL_HANDLE, hFid2);
    I've tried to debug in debug and release configuration but the change of g_ThLoop from Thread2 doesn't make Thread1 to exit
    also tried to use CmtLock, Local Variable and Safe Variable but...with no success
    Uriya

    How is your g_ThLoop variable defined?
    In Monitoring and Controlling Secondary Threads help topic the following note can be found:
    Note  If you use the volatile keyword, this code will function properly in an optimizing compiler such as Microsoft Visual C++. An optimizing compiler determines that nothing inside the while loop can change the value of the quit variable. Therefore, as an optimization, the compiler might use only the initial value of the quit variable in the whileloop condition. Use the volatile keyword to notify the compiler that another thread might change the value of the quit variable. As a result, the compiler uses the updated value of the quit variable each time the loop executes.
    Multitthreading is indeed a powerful technique but requires a non trivial knowledge of its inner aspects. I suggest you to carefully read Creating Multithreaded Applications topic in the CVI help: it gives the basics of multithreading and a lot of advanced features you should know to master this technique.
    Proud to use LW/CVI from 3.1 on.
    My contributions to the Developer Zone Community
    If I have helped you, why not giving me a kudos?

  • Global variable for function in JSP?

    Hi,
    In my JSP file I have a variable (sName) which I would like to use in a function. Is that possible? Do I have to declare it differently?
    Many thanks
    Simone
    Here the code fragement:
    <jsp:useBean id="s" class="ServiceRegistryClient"></jsp:useBean>
    <% Vector sName = s.getServiceNames(); %>
    <input type="Submit" name="callService" value="Call Service" onclick="callService()">
    <script language="javascript">
    function callService() {
    alert("Text" + sName.elementAt(0));
    }</script>

    Here's a sample jsp showing what I mean. It just posts back to itself:
    <HTML>
    <HEAD>
    <TITLE>Crappy JSP</TITLE>
    <%
    String element = null;
    java.util.StringTokenizer temp = null;
    String row = null;
    // get enumeration of parameters
    java.util.Enumeration names = request.getParameterNames();
    while (names.hasMoreElements()) {
      element = String.valueOf(names.nextElement());
      // get button name / row
      temp = new java.util.StringTokenizer(element, "_");
      if (temp.countTokens() == 2) {
        element = temp.nextToken(); // name
        row = temp.nextToken();  // row
      } else continue;
      // my button?
      if (element.trim().equals("callService")) {
           out.print("ROW NUMBER == " + row);
           break;
    java.util.Vector sName = new java.util.Vector();
    java.util.Vector sDescription = new java.util.Vector();
    java.util.Vector sLocation = new java.util.Vector();
    final int count = 3;
    for(int x = 0; x < count; x++) {
      sName.add("Name " + x);
      sDescription.add("Description " + x);
      sLocation.add("Location " + x);
    %>
    </HEAD>
    <BODY>
    <FORM action="../timesheet/Sample.jsp">
    <TABLE>
    <% for (int i = 0; i < count; i++) { %>
    <TR>
    <TD>
    <%= sName.elementAt(i) %>
    </TD>
    <TD>
    <%= sDescription.elementAt(i) %>
    </TD>
    <TD>
    <%= sLocation.elementAt(i) %>
    </TD>
    <TD>
    <input type="submit" name="callService<%= "_" + i %>" value="Call Service">
    </TD>
    </TR>
    <% } %>
    </TABLE>
    </FORM>
    </BODY>
    </HTML>

  • Set global variable in FOX formula

    Hi all,
    Does anybody know how to change the value of a global variable inside a FOX formula?.
    I can get its value and assign to a local variable but I would like to set the global value after reading a keyfigure and make some calculations.
    is it possible? maybe with some ABAP function call?
    Thanks for your help!

    Include characteristic HIRE in your 'fields to be changed' of planning function and in FOX write:
    DATA L_HIRE TYPE HIRE.
    DATA EXISTBPSID TYPE ZTA_BPSID.
    DATA BPSIDNO TYPE ZTA_BPSID.
    BPSIDNO = VARV(BPSTAS2).
    FOREACH L_HIRE.
    IF EXISTBPSID = '#'.
    {L_HIRE,BPSIDNO} = {L_HIRE,EXISTBPSID}.
    BPSIDNO = BPSIDNO + 0000000001.
    ENDIF.
    ENDFOR.

  • How can I pass a value field to a global variable?

    I have a doubt,
    I wnat to know if is possible to pass a value field to a global variable inside of DataFlow?
    That a try to do is at the time to execute a JOB on line (real time), depending of the value field that i receive, only certain DATAFLOWS will be executed  depending of the value field.
    I hope you can help me.
    Kind regards!!!

    It can be done via various ways, your approach is kind of odd, though.
    You are saying you have one kind if Input message, depending on the flag you want to do different transformations. The way you envision that is by loading that flag into a global variable and use different dataflows then.
    Can you use different dataflows within one Realtime Job? Yes, usually the first loads an in-memory datastore.
    Can you write into a global variable? Yes and No. Not directly, but you can write the value into e.g. a database and using the sql() function read it from there. But why would you. You could write it into the in-memory datastore. And then you run your n dataflows, each has a filter. So only one of these dataflows will actually process the data.
    Are there other approaches? I would have a Case transform at the beginning and route the data into different queries depending on the data. So all in one dataflow. Your approach is fine for a batch dataflow where a dataflow is started, does something and then ends. But that's not how it works in Realtime!

  • Global vairable in Function module

    Hello Friends,
    How to declare a global variable in function module, other than declaring in Function group/data section of function module.
    Regards
    Elini.P

    Hi Elini,
    You can declare the global data after the ENDFUNCTION statement in your function module if you wish, or you can include your own include in the TOP include.
    Actually you can declare global data just about anywhere in your function group as long as its not in between statments like FORM ENDFORM or FUNCTION ENDFUNCTION.
    I agree with Anand, though, why?
    Cheers,
    Brad

  • Change a global variable with a function

    HI ALL!
    I have the following problem...
    I would like to change a global variable with a void function so not like this: variable = function(); .
    What do you suggest?
    Best regards,
    Korcs

    I have the following problem...
    I would like to change a global variable with a void
    function so not like this: variable = function(); .
    What do you suggest?>
    >
    I dont quite understand your question but if your variable is a reference to an object then you can certainly have a method to alter the object that the reference is pointing to and/or the reference itself..
    otherwise your 'function' will have to return the same type as the variable itself in order to do the assignment as you demonstrated, regardless of wether it's C, C# or Java, AFAIK, WIRNM
    HTH

  • How to modify global variable in a function?

    Hello,
    I want to modify a globalvariable in a function, at first I did it this way:
    class Global_output_class
    GlobalDim("Correlation_Status,fail_part,End_Exp")
    dim pouet
    Correlation_Status = 12
    Call Correlation()
    pouet = Correlation_Status
    Function Correlation()
    Dim Global_output_class_sub
    Set Global_output_class_sub = new Global_output_class
    Correlation_Status = 1
    fail_part = 2
    End_Exp = 3
    Global_output_class_sub.CorrelationStatus = Correlation_Status
    Global_output_class_sub.failpart = fail_part
    Global_output_class_sub.EndExp = End_Exp
    set Correlation = Global_output_class_sub
    End function
    In this case: correlation_status receive the value 12, then I go to my function correlationn() where it became 1
    Then it goes out of the subfunction and takes the previous value from the program(12) ( I dont want that)
    To solve the problem I made it this way:
    class Global_output_class
    public CorrelationStatus
    public failpart
    public EndExp
    end class
    GlobalDim("Correlation_Status,fail_part,End_Exp")
    Correlation_Status = 12
    Set Global_Output = Correlation()
    Correlation_Status = Global_Output.CorrelationStatus
    fail_part = Global_Output.failpart
    End_Exp = Global_Output.EndExp
    pouet = Correlation_Status
    Function Correlation()
    Dim Global_output_class_sub
    Set Global_output_class_sub = new Global_output_class
    Correlation_Status = 1
    fail_part = 2
    End_Exp = 3
    Global_output_class_sub.CorrelationStatus = Correlation_Status
    Global_output_class_sub.failpart = fail_part
    Global_output_class_sub.EndExp = End_Exp
    set Correlation = Global_output_class_sub
    End function
    This way my global value are recopied in themselves after leaving the subprogram
    I got a lot of variables, is there any easier way so the global variable modified in a function keep the value after leaving the function?
    Thanks for help,
    Fred
    Solved!
    Go to Solution.

    Hi Fred,
    it is possible to use a global defined variable but the better way is to use to use a funtion call (or procedure call) with parameters. Please find first the good solution for a funcion call with parameter and the sub-optimal way with an global valiable:
    dim oParameter
    set oParameter = new cGlobal_output_class
    oParameter.Correlation_Status = 12
    msgbox "Correlation_Status before Call Correlation: " & oParameter.Correlation_Status
    Call Correlation(oParameter)
    msgbox "Correlation_Status after Call Correlation: " & oParameter.Correlation_Status
    Function Correlation(oPara)
    msgbox "Correlation_Status in the FUNCTION before change: " & oPara.Correlation_Status
    oPara.Correlation_Status = 1
    oPara.fail_part = 2
    oPara.End_Exp = 3
    msgbox "Correlation_Status in the FUNCTION after change: " & oPara.Correlation_Status
    End function
    class cGlobal_output_class
    dim Correlation_Status,fail_part,End_Exp
    end class
    call GlobalDim("oPouet")
    dim oPouet
    set oPouet = new cGlobal_output_class
    oPouet.Correlation_Status = 12
    msgbox "Correlation_Status before Call Correlation: " & oPouet.Correlation_Status
    Call Correlation()
    msgbox "Correlation_Status before Call Correlation: " & oPouet.Correlation_Status
    Function Correlation()
    msgbox "Correlation_Status in the FUNCTION before change: " & oPouet.Correlation_Status
    oPouet.Correlation_Status = 1
    oPouet.fail_part = 2
    oPouet.End_Exp = 3
    msgbox "Correlation_Status in the FUNCTION after change: " & oPouet.Correlation_Status
    End function
    class cGlobal_output_class
    dim Correlation_Status,fail_part,End_Exp
    end class
    Greetings
    Walter

  • Call function in LabView from a DLL, then access global variable from DLL

    I have created a DLL in LabWindows with a function and a structure.  I want to call the function from within LabView and then access the global structure.  I am able to call the function from the DLL with a "Call Library Function Node" and can access the return value, but I cannot figure out how to access the global structure.  The structure is declared in the DLL header file with __declspec(dllimport) struct parameters.
    Is there any way of accessing this structure without using the Network Variable Library?
    Solved!
    Go to Solution.

    dblok wrote:
    When you say "access to" or "the address of" the global variable, do you mean to pass the variable as an argument to the function call in the DLL?  If so, then I was not really sure how to pass a cluster from LabView by using the "Call Library Function Node".
    Yes, that's exactly right.  I would include a pair of helper functions in the DLL to read and write the global variable.  Alternatively you might write separate helper functions for each field inside the global structure, depending on the number of fields and whether you want to do any validation on the values.
    You can pass a cluster by reference to a function that expects a struct by setting the parameter to Adapt to Type, so long as the cluster does not contain any variable-length elements (strings or arrays).  The cluster needs to match the struct exactly, and sometimes that involves adding extra padding bytes to make the alignment work.  Variable-length elements in LabVIEW need to be converted to clusters containing the same number of elements as the struct definition (for example, if your struct contains char name[12], you would create a cluster of 8 U8 values, and you could use String to Array of Bytes followed by Array to Cluster to convert a LabVIEW string into that format).  If the struct contains pointers it gets more complicated, and it may be easier to write functions in the DLL to access those specific elements individually.
    If you can't get this working or need help, post your code and an explanation of the error or problem you're seeing.
    EDIT: it is also possible to include a single function in the DLL that returns the address of the global variable, which LabVIEW can then use to access and modify the data, but that's more complicated and likely to lead to crashes if you don't get the memory addressing exactly right.

  • Functional Global Variable in the CLAD sample Exam

    Hello,
    A question in the CLAD sample exam #2 keeps puzzling me. It's question number 32 about FGVs:
    32. Which is not an important part of creating a Functional Global Variable?
    a. Using Shift Registers to store information
    b. Changing the VI’s execution settings to Reentrant
    c. Setting the VI to inline into calling VIs
    d. Setting the While Loop to stop after one iteration
    The answer given is the following
    32.Correct Answer: B
    Topic: Functional Global Variables
    Justification: It is not necessary to inline functional global variables into their calling VIs. In fact, inlining requires that the subVI be reentrant, which is forbidden for functional global variables.
    Clearly, for an FGV to work properly, the VI must NOT be reentrant. So I would have selected answer B, but the justification seems to clearly point to answer C as well, doesn't it? Is it a typo, a subtlety in the wording? I am stuck on this one!
    Solved!
    Go to Solution.

    Well, inlining requires reentrant.  Therefore, the best answer should technically be C.  This looks like one of those rejected/retired questions since it isn't quite right and/or left up to too much interpretation.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines

  • Using a global variable within a function

    Hi all,
    I'm trying to learn how to use global variables, but can't quite get it right:
    Im trying to use two buttons, named UpButton and DownButton to control one global variable, p1
    A text field, named "Value" displays p1.
    Here's my code:
    p1 = 0;
    ButtonClick("UpButton", p1, 100,"Value");
    ButtonClick("DownButton", p1,(-100),"Value");
    function ButtonClick(ButtonName,Var,Increment,TextToChange){
                        sym.$(ButtonName).click(function(){
                        Var = Var + Increment;
                        sym.$(TextToChange).html(Var);
    However, it seems to be treating p1 as two different variables.
    Here's a link to the page to show you what I mean, apologies but this link wont work in google chrome because I'm using dropbox as a server temporarily
    In other browsers you may need to click "Show all content" or similar to any security warnings...
    https://dl.dropboxusercontent.com/u/83693404/ButtonTest/buttons.html
    Here are the project files:
    https://app.box.com/s/0shocivto8fl295h62uq
    Any help greatly appreciated, sorry if this is a stupid question,
    Katherine

    Hi Katherine,
    However, it seems to be treating p1 as two different variables.
    Indeed, your code created two different variables in two different scopes (I guess it as to do with JavaScript closures, but do  not ask me more, I am still a bit foggy about that idiosyncrasy ).
    In JavaScript, a primitive parameter – this is the case here with p1, a Number value –, is passed by value. In other words, a copy of the value is made. Your code handles two copies of the value stored in p1.
    This contrasts with an object parameter, which is passed by reference (address in memory), without copy. Your code would work if Numbers were passed by reference : the two Var (p1) would then point to the same address.
    The intent behind your trial is a global variable.
    1) Let us be as simple as possible :
    Stage : document.compositionReady event handler
    p1 = 0;
    buttonClick = function( Increment)
      p1 += Increment;
      sym.$( "Value").html( p1);
    Stage : DownButton.click event handler
    buttonClick(-100);
    Stage : UpButton.click event handler
    buttonClick(100);
    Without being preceded by the var keyword the variables are global.
    So the Number variable p1 can be accessed inside function buttonClick.
    And the Function variable buttonClick as well, is accessible inside click event handlers, everything (function definition +function calls) being inside the same symbol (Stage).
    2) Now, suppose this is no longer the case : you want to access a variable from another symbol.
    We create a new GraySquare symbol, instantiated both in the DownButton symbol (instance name DownSquare) and in the UpButton symbol (instance name DownSquare).
    Stage : document.compositionReady event handler
    p1 = 0;
    sym.buttonClick = function( Increment)
      p1 += Increment;
      sym.$( "Value").html( p1);
    The sym. prefix is now necessary to make the variable (here a Function, but you would proceed exactly the same with a String, Number or Boolean) accessible in other symbols.
    DownButton symbol : DownSquare.click event handler
    sym.getComposition().getStage().buttonClick( -100);
    UpButton symbol : UpSquare.click event handler
    sym.getComposition().getStage().buttonClick( 100);
    From these other symbols, the sym.getComposition().getStage(). prefix is necessary to acces to our global variable (function).
    The two examples are downloadable here : https://app.box.com/s/6vkyiqk7i8zwlw0j1wk1
    Gil

  • How does a global variable affect the runtime of optimisation-function fminsearch?

    Hi,
    my function "fmintest" needs 254s ~ 4min to finish calculation in Mathscript, while Matlab needs
    3s!!! I need the sorfware for data processing in a real time measuring system, where the calculation have to be done within 5 seconds.
    In the Mathscript-help I found the following (global -> details):
    If you call this function from a MathScript Node, LabVIEW operates with slower
    run-time performance for the MathScript Node. To optimize the performance of the
    MathScript Node, remove this function from scripts.
    How can I remove needed global variables in the fminsearch function handle? Is the system faster if I would/can remove the global variables?
    How can I avoid Mathscript-breakdown after saving changes to m-files and restarting them again?
    regards
    Attachments:
    ni3.7z ‏2 KB

    Hi,
    can you provid a VI and not only your m-files? It will be easier to help you!
    Can the customer provide a VI that demonstrates this behavior, rather than just providing the m files?
    Also, the Help file says fminsearch is not supported by the
    LabVIEW Run Time engine, so please take that in mind. If you want to build an application you cant use this function.
    Regards, RMathews

Maybe you are looking for

  • General xSan questions for a new setup

    So I'm looking to setup xSan. Had mainly one question. I know it's recommended that you have a secondary metadata controller server incase one goes down, but is this absolutely needed? We will have very few (like 3) people using the system so it won'

  • CS6 Bridge not opening after updating...

    I installed the latest update for Bridge and now it won't open. I am using Mountain Lion with 16GB of RAM. Photoshop CS6 still seems to be working fine . I started getting a message at startup about Bridge encountering a problem and needing to purge

  • Photoshop shutting down - error code (-600)

    Hi Adobes, I have had some problems recently with my new computer and adobe creativ suite 4. When im working on a new design or project, photoshop sometimes shuts down without warning, this is a major problem for me.. I asked someone in an apple rese

  • Infoset based on logical database CRC and customer field wrongly calculated

    Hello, I'm building an info-set (SQ02) based on logical database CRC. My requirement is to calculate a value based on the start date of the capacity allocated to a work center (KAKO-BEGZT). I defined a customer field (named FIELD1) with data type I,

  • Patch for developer6.0

    Hi can any one tell me the site address to download a patch for developer 6.0 thanks sanjay null