For i in loops on 2 arrays


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
Old 3 Weeks Ago
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; 3 Weeks Ago at 04:41 AM..
# 2  
Old 3 Weeks Ago
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:
Neo (2 Weeks Ago)
# 3  
Old 3 Weeks Ago
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

More UNIX and Linux Forum Topics You Might Find Helpful
Help with loops? jose2802 UNIX for Advanced & Expert Users 3 10-20-2014 11:54 AM
Dealing with Double Loops, Arrays and GREP A-V UNIX for Dummies Questions & Answers 10 05-30-2012 08:23 AM
Too many for loops maverick72 Shell Programming and Scripting 2 05-27-2011 07:36 AM
question about int arrays and file pointer arrays omega666 Programming 1 03-16-2011 12:15 AM
Two Loops cinderella Shell Programming and Scripting 8 01-20-2011 03:19 PM
Help with loops dsravan Shell Programming and Scripting 0 06-02-2010 08:00 PM
Help with for loops Oshie74 UNIX for Dummies Questions & Answers 5 03-14-2010 08:45 PM
Loops Shazin Shell Programming and Scripting 4 08-04-2009 09:09 AM
PHP arrays in arrays JerryHone Web Development 3 03-13-2009 02:19 PM
arrays and while loops in bash npatwardhan Shell Programming and Scripting 4 11-15-2008 12:36 PM
For Loops msb65 UNIX for Dummies Questions & Answers 3 08-06-2008 05:00 PM
trying to learn for loops, and arrays at the same time DeCoTwc Shell Programming and Scripting 3 05-14-2008 11:13 PM
While Loops vdc UNIX for Dummies Questions & Answers 4 05-10-2005 08:55 AM
Loops within loops bthomas Shell Programming and Scripting 8 04-14-2005 01:09 PM
korn shell "loops & arrays" muzica Shell Programming and Scripting 7 09-23-2004 03:02 PM