Problem with arrays


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with arrays
# 1  
Old 10-20-2010
Problem with arrays

Hi

I have two arrays:

arr1 = (demo demo2 demo3 demo4 demo5)
arr2 = (demo2 test demo)

I want to check that the values the "arr2" are present in "arr1"

Example

arr1 = (demo demo2 demo3 demo4 demo5)
arr2 = (demo2 test demo)

Output: Error

arr1 = (demo demo2 demo3 demo4 demo5)
arr2 = (demo2 demo5)

Output: OK

Thanks for your help SmilieSmilieSmilieSmilie
# 2  
Old 10-20-2010
What shell are you using? What have you tried?
# 3  
Old 10-20-2010
Quote:
Originally Posted by Corona688
What shell are you using? What have you tried?
I using bash.

Code:
#!/bin/bash

arr1="demo demo2 demo3 demo4 demo5"
arr2=$(echo 'demo2,test,demo' | sed 's/\.*,/ /g')

for i in $arr2
do
   list="$list $i.out"
done

echo $list

The second array is assembled from a list of parameters passed, I have the arrays ok, but do not know how to verify that the values the "arr2" are present in "arr1" as I said in the beginning of the post.

Thank
# 4  
Old 10-20-2010
Those aren't arrays at all, for starters.

Code:
arr1=( demo demo2 demo3 demo4 demo5 )
OLDIFS="${IFS}"
# Special variable that controls how shell splitting works
IFS=","
VAR="demo2,test,demo"
# Will split apart on IFS
arr2=($VAR)
# Change it back since important things can break if we don't
IFS="${OLDIFS}"

for ((N=0; N<${#arr1[@]}; N++))
do
        echo "arr1[$N] = ${arr1[$N]}"
done

Another loop inside, on arr2, will let you check every element against every other element.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using arrays?

I have never used arrays before but I have a script like this: var1=$(for i in $(cat /tmp/jobs.021013);do $LIST -job $i -all | perl -ne 'print /.*(\bInfo.bptm\(pid=\d{3,5}).*/' | tr -d "(Info=regpid" | tr -d ')'; $LIST -job $i -all | cut -f7 -d','| sed -e "s/^\(*\)\(*\)\(*\)\(.*\)/\1... (2 Replies)
Discussion started by: newbie2010
2 Replies

2. Programming

Arrays in C++

I've noticed something interesting in C++ programming. I've always done tricky stuff with pointers and references to have functions deal with arrays. Doing exercises again out of a C++ book has shown me an easier way, I didn't even know was there. It's weird to me. When dealing with arrays, it... (4 Replies)
Discussion started by: John Tate
4 Replies

3. Programming

problem in multiplying arrays

Hi, this is my code.It's simple : there are 2 2D arrays and the multiplied to C. #include<stdio.h> #include<sys/shm.h> #include<sys/stat.h> #include<stdlib.h> main() { int *A; //A int *B; //B int *C; //C int i,j,x,k,d; int id; ... (17 Replies)
Discussion started by: giampoul
17 Replies

4. Shell Programming and Scripting

Problem with arrays and loop

Hello , im sorry for my english . im trying to create a dynamic menu that will display if the interface is ACTIVE OR STOPPED/FAILED for some reason i cant get it to work properly start_interface_func() { i=0 for interface_chk in 11 71 73 72 12 47 48 49 50 20 23 24 25 46 21 22 27 28... (5 Replies)
Discussion started by: visiown
5 Replies

5. Shell Programming and Scripting

How can I use the arrays ?

Hi all, I have a file test1.txt with the below contents abc def ghj xyz I tried printing these values using arrays. Script tried : =========== set -A array1 `cat test1.txt` count=${#array1 } i=0 while do echo "element of array $array1" done (1 Reply)
Discussion started by: dnam9917
1 Replies

6. 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

7. UNIX for Dummies Questions & Answers

Problem assigning variables to arrays

Hi All, I have a problem assigning variables to script.I have a script in which i have a while loop now i have to assign some values obtained to an array which will be used later in the script.Can anyone help how to do that. At present my scrot looks like: co=0 pco=0 co=`cat /tmp/highcpu... (4 Replies)
Discussion started by: usha rao
4 Replies

8. 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

9. Programming

C programming + problem with char arrays

Im trying to write some code atm which gets the complete pathname of a folder and strips off references to the parent folders. The end result should be just the name of the folder. Currently Im able to extract the folder name, however Im getting junk added onto the name as well which is making... (7 Replies)
Discussion started by: JamesGoh
7 Replies

10. Shell Programming and Scripting

Problem with arrays in awk

Hello! I'm trying to make a script that will make a list of the files in a source tree and sort them by size. Problem is I've run into a weird problem. print array will give me numbers like 160, 220, 444 that i don't even know where they come from, and print array will give me the correct numbers... (5 Replies)
Discussion started by: Glauco
5 Replies
Login or Register to Ask a Question