I am splitting up my code into separate files, but have a problems with variables within the sub files (I am sure it's something simple, but cannot work out what is wrong.  When I run the code I get an "UnboundLocalError: local variable 'avglightlevel' referenced before assignment" error.  I thought the variables would be local to the sub file, I have tried using global variables in each file and still cannot it working.  How do I a variable in the sub file to keep a running total of the avg values?  #main.py import datacalc  datacalc.init(0)   #Read sensors &amp; process the data datacalc.complielightlevel(lightlevel)  # delay for 10 seconds and keep repeat   ====================================================== #datacalc.py def init(avg):        avglightlevel = 0        avgCO2level = 0       avgtemperaturelevel = 0  #================================================    def complielightlevel(lightlevel):     if(lightlevel &gt; 0):           avglightlevel = avglightlevel + lightlevel
4613
How can I create or use a global variable in a function?  If I create a global variable in one function, how can I use that global variable in another function? Do I need to store the global variable in a local variable of the function which needs its access?
100903
Most languages have naming conventions for variables, the most common style I see in shell scripts is MY_VARIABLE=foo.  Is this the convention or is it only for global variables?  What about variables local to the script?
77595
DATA SET file name "demo.txt"  ID|SAL|COL|PER|TAG|GER 1"|"1.11"|"2.22"|"1.1"|"2.2"|D" 2"|"1.234"|"3.234"|"2.2222"|"2.34"|"B" 3"|"1.234"|"35.23"|"3.2"|"2.34"|"A"   I have created a function which will automatically calculate decimal column sum below is the function I am calling that function in main.sh script   demo() { FILE_NAME="$1" COLUMN_NAME="$2"  alpha=( $(awk -F"|" 'NR==1{for(i=1;i&lt;=NF;i++){if ($i ~ /'$COLUMN_NAME'/){print i;}}}' $FILE_NAME) )  for each in "${alpha[@]}" do    awk -F'"?\\|"?' '{T+=$('$each')} END { printf "%.2f\n", T }' $FILE_NAME   done  }   Script : main.sh   #function called in main.sh script   demo demo.txt 'SAL|COL|PER|TAG'   issue is how do i get the output into an array   below is the output i am getting need to store into array1   3.57 40.68 6.52 6.88   second array is this below output getting from another program   array2={3.57 40.68 6.52 6.88}   **so that I can match the array1 with array 2 position value    array1[0] with array2[0]  array1[1] with array2[1]  array1[2] with array2[2]  array1[3] with array2[3]    if all succeed should display success status as YES  i have solution which is partially working ... help me with some solution  so that i can store each value of an array to array1   FILE_NAME="$1" COLUMN_NAME="$2"  alpha=( $(awk -F"|" 'NR==1{for(i=1;i&lt;=NF;i++){if ($i ~ /'$COLUMN_NAME'/){print i;}}}' $FILE_NAME) )  declare -a array=();  for each in "${alpha[@]}" do         #var=($(awk -F'"?\\|"?' '{T+=$('$each')} END { printf "%.2f\n", T }' $FILE_NAME))         mapfile -t array &lt;&lt;&lt; "$(awk -F'"?\\|"?' '{T+=$('$each')} END { printf "%.2f\n", T }' $FILE_NAME)"         #awk -F'"?\\|"?' '{T+=$('$each')} END { printf "%.2f\n", T }' $FILE_NAME done  echo "${array[@]}"
4177
Is there a way to step between 0 and 1 by 0.1?   I thought I could do it like the following, but it failed:  for i in range(0, 1, 0.1):     print i   Instead, it says that the step argument cannot be zero, which I did not expect.
3567
Given the following code, what does the if __name__ == "__main__": do?  # Threading example import time, thread  def myfunction(string, sleeptime, lock, *args):     while True:         lock.acquire()         time.sleep(sleeptime)         lock.release()         time.sleep(sleeptime)  if __name__ == "__main__":     lock = thread.allocate_lock()     thread.start_new_thread(myfunction, ("Thread #: 1", 2, lock))     thread.start_new_thread(myfunction, ("Thread #: 2", 2, lock))
34071
Consider this:  public class TestClass {      private String a;     private String b;      public TestClass()     {     a = "initialized";     }      public void doSomething()     {     String c;          a.notify(); // This is fine     b.notify(); // This is fine - but will end in an exception     c.notify(); // "Local variable c may not have been initialised"     }  }   I don't get it. "b" is never initialized but will give the same run-time error as "c", which is a compile-time error. Why the difference between local variables and members?  Edit: making the members private was my initial intention, and the question still stands...
123679
Two function definitions, the only difference is that the first combines the local storage keyword with the assignment, while the second separates them:  function foo {     local fn=$(mktemp -p /path/does/not/exist 2&gt;/dev/null)     echo $? }  function bar {     local fn     fn=$(mktemp -p /path/does/not/exist 2&gt;/dev/null)     echo $? }  foo bar   This echoes "0" then "1". I expect it to echo "1" then "1". It seems like the value of $? is the result of the assignment to local, rather than the result of the command substitution.  Why does bash 4.2.46(1)-release behave this way?
3866
Is it possible to have static class variables or methods in Python? What syntax is required to do this?
102311
I'm trying to create a new variable using the value of an existing variable as part of the variable name.  filemsg"$word1"=" "   I've also tried   filemsg$word1=" "  filemsg${word1}=" "   on all attempts I get the following when that line executes,  cicserrors.sh[45]: filemsgCICS= : not found [No such file or directory]
80124
I'd like to change the value of a user-defined project variable (can be manually edited in Project Settings | Variables) using the Python console. I tracked down the  but haven't succeeded in actually changing the variable in the project settings. My code so far:  iface.mapCanvas().mapSettings().expressionContext().scope(0).setVariable('myvar',1)   I guess I'm getting lost in the different expression contexts ...
1704
This has always confused me. It seems like this would be nicer:  my_list = ["Hello", "world"] print(my_list.join("-")) # Produce: "Hello-world"   Than this:  my_list = ["Hello", "world"] print("-".join(my_list)) # Produce: "Hello-world"   Is there a specific reason it is like this?
23359
I'll appreciate your help with the following issue:  I'm trying to set an array which contains a variable as part of the array name, example: Arr_$COUNTER (where $COUNTER is changed based on a loop count)  Every possible way I have tried came up with an error, such as "bad substitution" or "syntax error near unexpected token"  Here is the entire flow:   There is one file which contain multiple lines. each line has 6 values separated by space  10 20 30 40 50 60   100 200 300 400 500 600  The script, is meant to read each line from the file, and declare it as an array (with the line number which is the variable. as a test, each value should be printed and eventually another function will be executed on each value.  #!/bin/bash COUNTER=1 LINES=`wc -l VALUES_FILE.txt | awk '{print $1}'` echo "Total number of lines "$LINES echo while [ $COUNTER -le $LINES ] do echo "Counter value is $COUNTER" field=`awk "NR == $COUNTER" VALUES_FILE.txt` echo "Field = $field" declare -a "arr$COUNTER=($field)" echo "arr$COUNTER[0] = ${arr$COUNTER[0]}" echo "arr$COUNTER[1] = ${arr$COUNTER[1]}" echo "arr$COUNTER[2] = ${arr$COUNTER[2]}" echo "arr$COUNTER[3] = ${arr$COUNTER[3]}" echo "arr$COUNTER[4] = ${arr$COUNTER[4]}" echo "arr$COUNTER[5] = ${arr$COUNTER[5]}" let COUNTER=COUNTER+1 echo done echo "The End" echo    Here is the result:   Total number of lines 2  Counter value is 1 Field = 10 20 30 40 50 60 ./sort.sh: line 12: arr$COUNTER[0] = ${arr$COUNTER[0]}: bad substitution The End    What should be changed / fixed in order to have it working properly?  thank !
43815
So I found this python script on another post here and was trying to adjust it for my needs.  I'm a very novice python user so I'm struggling with how to modify the script.  I have a Feature Class stored in a feature Dataset that I want to search a field for duplicate values and populate a new field with Y for duplicate or N for none.  The below script is what I found that looks like it will work once I find a way to drill down into my file geodatabase.          from arcpy import *          inShapefile = pointsShapefile          checkField = "xyCombine"         updateField = "dplicate"         #List of values found once         occursOnce = []        #list of values found twice         occursTwice = []       cursor = da.SearchCursor (inShapefile, [checkField])           for row in cursor:           #Check value is not null        if row[0]:     #If not already found to occur twice, proceed     if not row[0] in occursTwice:         #If hasn't occured once yet         if not row[0] in occursOnce:             #Add to occurs once list             occursOnce.append (row[0])         #If value has already been found once         else:             #Add to occurs twice list (duplicates)             occursTwice.append (row[0])     del cursor       cursor = da.UpdateCursor (inShapefile, [checkField, updateField])      for row in cursor:       #Check value is not null         if row[0]:            #check if value in occursTwice list (i.e. is duplicate)     if row[0] in occursTwice:         row[1] = "Y"     else:         row[1] = "N"     cursor.updateRow(row)    del cursor
58895
Been trying to this line to parse for a good 20mins, but all attempts have failed:  \beta = $-$0.84, t = $-$2.09, P = 0.04   Could anyone tell me why I'm getting a Missing $ inserted error here?
23786
I'm trying to define a static variable for use in my custom module. I want it to let me pass a number from function to function. I'm using drupal_static() but I can't get it to work. How do I do this?  This is the code I'm using to define my static variable.  $var = &amp;drupal_static(__FUNCTION__); if (!isset($var)) {   // generate contents of static variable   $var = '8'; } return $var;   I've tried putting it in a custom module.    function viewusercontact_locateuser($value, $key, &amp;$counter) {    global $user;     /**********In here***********/     if ($value['uid'] == $user-&gt;uid) {       ........    } }   I can't seem to get it to work. What am I doing wrong?  (I got my code from the following guides)
107068
How to refer variable defined in sub shell in parent shell. command grouping is difficult in my case. What would be the alternative.
79650
I have a series of Python classes in a file. Some classes reference others.  My code is something like this:  class A():     pass  class B():     c = C()  class C():     pass   Trying to run that, I get NameError: name 'C' is not defined. Fair enough, but is there any way to make it work, or do I have to manually re-order my classes to accommodate? In C++, I can create a class prototype. Does Python have an equivalent?  (I'm actually playing with Django models, but I tried not complicate matters).