Note that there are some explanatory texts on larger screens.

plurals
  1. POShell Function to add elements to array not working when placed inside another function
    text
    copied!<p>I wrote a simple array in KSH 88 and did the following:</p> <pre><code># Vector methods set -A ZeroFilesArr GlobalIndex=-1 # Add element at the end function ZeroFilesArr.push_back { let GlobalIndex=$(($GlobalIndex + 1)) ZeroFilesArr[$GlobalIndex]=$1 return } # Return size function ZeroFilesArr.size { echo ${#ZeroFilesArr[@]} } # Clear content function ZeroFilesArr.clear { set -A ZeroFilesArr let GlobalIndex=$((-1)) } # Print contents in format # Var1 \t Var2 \t ... \t Var_n function ZeroFilesArr.print { local tempString for i in ${ZeroFilesArr[@]} do tempString="$tempString $i \t " done echo $tempString } </code></pre> <p>I tested afterwards using the following:</p> <pre><code>ZeroFilesArr.push_back File1 ZeroFilesArr.push_back File2 ZeroFilesArr.push_back File3 echo $(ZeroFilesArr.size) ZeroFilesArr.print ZeroFilesArr.clear echo $(ZeroFilesArr.size) ZeroFilesArr.push_back File1 ZeroFilesArr.push_back File2 echo $(ZeroFilesArr.size) ZeroFilesArr.print ZeroFilesArr.clear </code></pre> <p>The output is the following</p> <p><strong># kernel_coverage.sh</strong></p> <p><strong>3</strong></p> <p><strong>File1 File2 File3</strong></p> <p><strong>0</strong></p> <p><strong>2</strong></p> <p><strong>File1 File2</strong></p> <p>But afterwards I did the following:</p> <pre><code># Kernel Objects # KSH Array &lt;&lt;FILES&gt;&gt; Contains strings that represent # the objects that are published in the Kernel set -A FILES $(ls /dev/daKernel/) function CEILING { local DIVIDEND=$1 local DIVISOR=$2 let RESULT=$((((${DIVIDEND} + ${DIVISOR}) - 1)/${DIVISOR})) echo ${RESULT} } # Checks if the Parameter passed is # a published object of the Kernel and returns # the file size # Returns a string with the size function ZERO_FILE_CHECK { local kernel_path="/dev/daKernel" local FILE_NAME="$kernel_path/$1" local retval=$(ls -l ${FILE_NAME} | awk {'print $5'}) echo $retval } # Checks and returns a statistic of how # many files are in zero in the filesystem # I.E. 3 out of 100 would return 3 function ZERO_FIlES_CHECK { local let FILES_SIZE=${#FILES[@]} local COUNTER=0; for f in "${FILES[@]}" do local let n=$(ZERO_FILE_CHECK $f) if [ $n -eq 0 ]; then let COUNTER=$(($COUNTER+1)) fi done local PERCENTAGE=0 let PERCENTAGE=$(($COUNTER * 100)) let PERCENTAGE=$(CEILING $PERCENTAGE $FILES_SIZE) echo $PERCENTAGE } # Check if the files are not in zero # Input: An expected percentage, if bigger than # this then there are too many files in zero size # Depends on: ZERO_FIlES_CHECK and the FILES array # Returns: 0 (Success) or 1 (Failure) function CHECK_FILES { local let default_percentage=$1 local let ZEROFILE_PERCENTAGE=$(ZERO_FIlES_CHECK) if [[ $ZEROFILE_PERCENTAGE -ge $default_percentage ]]; then return 0; else return 1; fi } </code></pre> <p>And it worked too, it returns me whether a folder has too much files in zero or not. So I though it was all set and on the road to happiness riding a wild broncunicorn.</p> <p>But I was wrong.</p> <p>I placed my magical push_back inside the ZERO_FIlES_CHECK function like the following:</p> <pre><code>function ZERO_FIlES_CHECK { local let FILES_SIZE=${#FILES[@]} local COUNTER=0; for f in "${FILES[@]}" do local let n=$(ZERO_FILE_CHECK $f) if [ $n -eq 0 ]; then let COUNTER=$(($COUNTER+1)) ZeroFilesArr.push_back $f fi done local PERCENTAGE=0 let PERCENTAGE=$(($COUNTER * 100)) let PERCENTAGE=$(CEILING $PERCENTAGE $FILES_SIZE) echo $PERCENTAGE } </code></pre> <p>And tested it using forcing it to find even the slightest file in zero</p> <pre><code>if CHECK_FILES 0; then ZeroFilesArr.print echo $(ZeroFilesArr.size) fi ZeroFilesArr.clear if CHECK_FILES 0; then ZeroFilesArr.print echo $(ZeroFilesArr.size) fi </code></pre> <p>But it always prints the following:</p> <p><strong># kernel_coverage.sh</strong></p> <p>** An empty space here indicating NULL**</p> <p><strong>0</strong></p> <p>** An empty space here indicating NULL**</p> <p><strong>0</strong></p> <p>After trying several ways I am burned out, it seems I can't come up with a clear solution</p> <p>Any help would be greatly appreciated</p> <p>I'm thinking that I have a problem with variable scoping, but I though the global were globals just like anywhere else.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload