Today (Saturday) We will make some minor tuning adjustments to MySQL.

You may experience 2 up to 10 seconds "glitch time" when we restart MySQL. We expect to make these adjustments around 1AM Eastern Daylight Saving Time (EDT) US.


For i in loops on 2 arrays


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
For i in loops on 2 arrays

Hey ,

i have this script and i have these loops so it can find a match between 2 arrays :

Code:
ARRAY_1=(one two three)
ARRAY_2=(A B C)
VAR='B'

for NUMBERS in "${ARRAY_1[@]}"
do
        for LETTERS in "${ARRAY_2[@]}"
        do
        if      [[ $VAR == *"$LETTERS"* ]];then
                VAR='LETTERS'
                break
        fi
        if [[ $VAR == *"$NUMBERS "* ]];then
        VAR='NUMBERS'
        break

        else
        VAR='DEFAULT'

        fi
        done
done

echo "$VAR"

the thing is that the loops dont break!
after it fines a match just keep looping and gets to default:

Code:
+ ARRAY_1=(one two three)
+ ARRAY_2=(A B C)
+ VAR=B
+ for NUMBERS in '"${ARRAY_1[@]}"'
+ for LETTERS in '"${ARRAY_2[@]}"'
+ [[ B == *\A* ]]
+ [[ B == *\o\n\e* ]]
+ VAR=DEFAULT
+ for LETTERS in '"${ARRAY_2[@]}"'
+ [[ DEFAULT == *\B* ]]
+ [[ DEFAULT == *\o\n\e* ]]
+ VAR=DEFAULT
+ for LETTERS in '"${ARRAY_2[@]}"'
+ [[ DEFAULT == *\C* ]]
+ [[ DEFAULT == *\o\n\e* ]]
+ VAR=DEFAULT
+ for NUMBERS in '"${ARRAY_1[@]}"'
+ for LETTERS in '"${ARRAY_2[@]}"'
+ [[ DEFAULT == *\A* ]]
+ VAR=LETTERS
+ break
+ for NUMBERS in '"${ARRAY_1[@]}"'
+ for LETTERS in '"${ARRAY_2[@]}"'
+ [[ LETTERS == *\A* ]]
+ [[ LETTERS == *\t\h\r\e\e* ]]
+ VAR=DEFAULT
+ for LETTERS in '"${ARRAY_2[@]}"'
+ [[ DEFAULT == *\B* ]]
+ [[ DEFAULT == *\t\h\r\e\e* ]]
+ VAR=DEFAULT
+ for LETTERS in '"${ARRAY_2[@]}"'
+ [[ DEFAULT == *\C* ]]
+ [[ DEFAULT == *\t\h\r\e\e* ]]
+ VAR=DEFAULT
+ echo DEFAULT
DEFAULT

why ?!

i want it to work this way :
Code:
+ for NUMBERS in '"${ARRAY_1[@]}"'
+ for LETTERS in '"${ARRAY_2[@]}"'
+ [[ B == *\A* ]]
+ [[ B == *\o\n\e* ]]
+ [[ B == *\B ]]
+ [[ B == *\T\W\O* ]]
+ [[ B == *\C* ]]
+ [[ B ==  *\t\h\r\e\e* ]]


Last edited by batchenr; 02-27-2019 at 05:41 AM..
# 2  
It would seem to be because you have nested loops instead of having sequential loops. And, break with no operand only breaks out of the nearest enclosing loop. To break out of two nested loops you would need to use break 2.

Furthermore, the code you have (with nested loops) isn't testing A, B, C, one, two, three; it is testing A, B, C, one, A, B, C, two, A, B, C, three which doesn't seem necessary.

But, even if you fix that, I'm not sure why you're creating arrays and using loops. It looks like a simple case statement would be a better approach:
Code:
VAR=${1:-B}
case "$VAR" in
	(*A*|*B*|*C*)
		VAR=LETTERS;;
	(*one*|*two*|*three*)
		VAR=NUMBERS;;
	(*)	VAR=DEFAULT
esac
echo "$VAR"

If you insist on doing it with loops and arrays you would need to make the loops sequential instead of nested:
Code:
VAR=${1:-B}
found=0

ARRAY_1=(one two three)
ARRAY_2=(A B C)

for LETTERS in "${ARRAY_2[@]}"
do	if [[ $VAR == *"$LETTERS"* ]]
	then	VAR=LETTERS
		found=1
		break
        fi
done
if [[ $found == 0 ]]
then	for NUMBERS in "${ARRAY_1[@]}"
	do	if [[ $VAR == *"$LETTERS"* ]]
		then	VAR=NUMBERS
			found=1
			break
		fi
	done
fi
if [[ $found == 0 ]]
then	VAR=DEFAULT
fi

echo "$VAR"

This User Gave Thanks to Don Cragun For This Post:
# 3  
The reason for the undesired behaviour that you complain about is easily seen in the trace log given in your post:
$VAR's initial contents "B" is compared exactly once - with "A". Not equal - so VAR is assigned "DEFAULT" which is compared against from now, a NEVER will be equal to any of the array elements as given.
Don't use the same variable for (initial) input values and for the results unless you know exactly what you are doing.

Don Cragun showed some way simpler methods to solve your problem - try one of those...
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Dealing with Double Loops, Arrays and GREP

Can someone please help me to learn how to deal with loops, arrays and grep? I have two arrays (lets say I and j) each in a separate file And have file with lines of data I need to extract, such as Ruby Smith: some text here Ruby Smith: some other text here Ruby Brown: some text here Ruby... (10 Replies)
Discussion started by: A-V
10 Replies

2. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

3. Shell Programming and Scripting

Loops

Hi All, I want to execute a script the number of times a user enters. Please can you advise on hor can I do the same. Many Thanks, Shazin (4 Replies)
Discussion started by: Shazin
4 Replies

4. Web Development

PHP arrays in arrays

PHP question... I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc So what I want to do is display the... (3 Replies)
Discussion started by: JerryHone
3 Replies

5. Shell Programming and Scripting

arrays and while loops in bash

hi guys, i have an array called ARRAY which has elements in it... i am trying to assign elements of ARRAY to master_array.. i get a =: command not found error.. i=0 while do ${master_array}=${ARRAY} ((i++)) done is there something i am missing? (4 Replies)
Discussion started by: npatwardhan
4 Replies

6. Shell Programming and Scripting

trying to learn for loops, and arrays at the same time

Ok, I've already completed the task this is for, but now I'm trying to go back and find more eloquent solutions for future reference. I have a report I've generated that is formatted like this: 1033 1 1079 4 1453 5 2205 6 1933 7 461 8 646 9 1655 12 975 13 1289 14 The first number is... (3 Replies)
Discussion started by: DeCoTwc
3 Replies

7. Shell Programming and Scripting

Help with the 2 for loops

#!/bin/bash IFS=$'\n' A= a c b t g j i e d B= t y u i o p counter=0 found="" for i in $(cat $A) do for j in $(cat $B) do if then found="yes" fi done if then (1 Reply)
Discussion started by: vadharah
1 Replies

8. UNIX for Dummies Questions & Answers

While Loops

I'm trying to create a loop that will prompt the user for 15 values, not forcing them to enter all 15. If the user enters through one or more of the prompts the null value needs to be converted to 0, otherwise set the parameter = to the value entered: ex. Please enter file no #1: 17920 ... (4 Replies)
Discussion started by: vdc
4 Replies

9. Shell Programming and Scripting

Loops within loops

I am running on HPUX using ksh. I have a script that uses a loop within a loop, for some reason the script seems to hang on a particuliar record. The record is fine and hits the condition in Blue. If I kill the 1st loop process the script continues on with no problem. Begin code> <Some... (8 Replies)
Discussion started by: bthomas
8 Replies

10. Shell Programming and Scripting

korn shell "loops & arrays"

Hi, I am trying to write a script which will loop until a certain action has been performed. I have two files i would like to compares. For example: file1 has a list of user ids (about 900) from the company's e-mail server. file2 has a list of user ids (about 50 or so) from... (7 Replies)
Discussion started by: muzica
7 Replies

Featured Tech Videos