LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-18-2012, 01:02 AM   #1
Dick Dastardly
LQ Newbie
 
Registered: Jul 2011
Posts: 23

Rep: Reputation: Disabled
(BASH) Looping over an array


Some more variable stuff i'm afraid!

Is it possible to loop over an array (i think that's what it's called) and act on a specific variable?

For example, i would like to find all variables that DO NOT have XXX in it and append ZZZ to them.

Code:
_var=(1 2 3 4XXX)
echo ${_var[@]}|grep -v "XXX"
Doesn't seem to work and I can't get a for loop to either.
Can anyone prod me in the right direction?
Thanks muchly

Last edited by Dick Dastardly; 01-18-2012 at 01:04 AM.
 
Old 01-18-2012, 02:05 AM   #2
xylos
LQ Newbie
 
Registered: Oct 2011
Location: USA
Distribution: Backtrack 5, Ubuntu 11.10, Linux Mint 12, Cyanogen Mod 7, FreeBSD 9.0
Posts: 17

Rep: Reputation: Disabled
Something along these lines (assuming it's not a sparse array):

Code:
ARRAY=( 1 2 3 4XXX )
declare -i MAXNUMBER=5
declare -i LOOPCONTROL=0
while [ $LOOPCONTROL -lt $MAXNUMBER ]; do
   if [ `echo ${ARRAY[$LOOPCONTROL]} | grep -c "XXX"` -ne 0 ]; then
      TEMPVALUE=`echo ${ARRAY[$LOOPCONTROL]}`
      ARRAY[$LOOPCONTROL]="$TEMPVALUE""ZZZ"                       # Edited to change this to ZZZ instead of XXX as per OP
   fi
done
Just off the top of my head... haven't tried it on a box (stuck on a Windows machine at the moment), but that should do what you're looking for (in horribly inefficient but hopefully very readable syntax).

Last edited by xylos; 01-18-2012 at 02:08 AM. Reason: Correction to match OP intent
 
Old 01-18-2012, 04:24 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,655

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
How about:
Code:
#!/bin/bash

_var=(1 2 3 4XXX)

for i in ${!_var[*]}
do
	[[ ${_var[i]} =~ XXX ]] || _var[i]+=ZZZ
done

printf "%s\n" "${_var[@]}"
 
Old 01-18-2012, 04:32 AM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Quote:
Originally Posted by Dick Dastardly View Post
For example, i would like to find all variables that DO NOT have XXX in it and append ZZZ to them.
Usually it is easiest to first copy the array to a temporary variable, and rebuild a new array in a loop. You can also use Bash string manipulation to see if the string contains a fixed substring (or glob pattern):
Code:
original=(1 2 3 4XXX)

temporary=("${original[@]}")
original=()
for one in "${temporary[@]}" ; do
    if [ "$one" = "${one/XXX/}" ]; then
        one="${one}ZZZ"
    fi
    original=("${original[@]}" "${one}")
done
unset temporary

echo "${original[@]}"
will yield
Code:
1ZZZ 2ZZZ 3ZZZ 4XXX
The if clause verifies that you cannot remove XXX from the string. (If the string equals the same string with XXX removed, the string does not contain XXX, and therefore we append ZZZ to it.)
 
1 members found this post helpful.
Old 01-18-2012, 04:36 PM   #5
Dick Dastardly
LQ Newbie
 
Registered: Jul 2011
Posts: 23

Original Poster
Rep: Reputation: Disabled
Thanks for the help guys. I'll have a look at this later tonight. =]
 
Old 01-21-2012, 10:35 PM   #6
Dick Dastardly
LQ Newbie
 
Registered: Jul 2011
Posts: 23

Original Poster
Rep: Reputation: Disabled
@Nominal Animal suggestion worked for me. Thank you for the help.

2 questions with that code:
1) What does the "original=()" do?
2) What do the slashes do in the if test "if [ "$one" = "${one/XXX/}" ]; then"?
Thanks.
 
Old 01-22-2012, 08:37 AM   #7
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Quote:
Originally Posted by Dick Dastardly View Post
1) What does the "original=()" do?
It sets original to an empty array.

Quote:
Originally Posted by Dick Dastardly View Post
2) What do the slashes do in the if test "if [ "$one" = "${one/XXX/}" ]; then"?
It is a string operation, described in detail in the Bash Reference Manual (Shell Parameter Expansion chapter).

Put simply, ${variable/pattern/replacement} evaluates to the value of variable but with the first pattern replaced by replacement.

There are other similar expressions explained in the link above. For example, ${variable//pattern/replacement} evaluates to the value of variable but all patterns replaced by replacement.

Note that the pattern is actually a glob pattern, similar to the patterns you use to specify file name patterns. Its syntax is explained in the Pattern Matching section in the Bash Reference Manual.

It is easy confuse these glob pattern string expressions with regular expressions, which look similar, but have different special characters. It is a good thing you asked.
 
1 members found this post helpful.
  


Reply

Tags
array, bash, loop, variable


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 11:07 AM
Looping issue in bash scripting ZAMO Linux - General 4 09-26-2008 12:22 PM
x86 assembly -- problem w/ syntax for looping over array jrtayloriv Programming 7 02-01-2008 09:46 AM
Looping a bash script SoulGrind Linux - Newbie 5 09-22-2006 07:03 PM
Bash case structure (looping) ravvar Programming 4 10-07-2003 08:07 PM


All times are GMT -5. The time now is 12:25 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration