Populating an Array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Populating an Array
# 1  
Old 07-16-2009
Populating an Array

Guys,

I need to iterate populate an array while going over files in directory. Can someone please tell me syntax I tried this but it isn't working ==>

Code:
for F in `ls -p "${directory1}" | grep -v "\/"`
do 
  cd "${directory2}"
    cmp  "${directory2}"/"${F}"  "${directory1}"/"${F}"  >/dev/null;REPLY=$?
  if [ ${REPLY} -eq 0 ]
  then
        file_arr_idd=($F)
	echo "$file_arr_idd[0]"
  else
        file_arr_diff=($F)
  fi
done

# 2  
Old 07-16-2009
arr=(a b c)
but you can't add element using this method (I think, not checked).

Method 1, works with every posix-sh
init cnt:
idd=1
diff=1

using:
file_arr_idd[$idd]=$F
(( idd+=1 ))

same way diff array
file_arr_diff[$diff]=$F
(( diff+=1 ))

Method 2, assosiative arrays in ksh
init array:
typeset -A file_arr_idd file_arr_diff
using ex:
file_arr_idd[$F]=1
file_arr_diff[$F]=1

print out used array id, ex:
Code:
for f in ${!file_arr_idd[*]}
do   
     echo $f
done

# 3  
Old 07-16-2009
Hey kshji!! I just completed my Program I used the follwoing method to populated and run my array:

Code:
set -A file_arr_diff 
set -A file_arr_idd 
i=0
j=0


for F in `ls -p "${directory1}" | grep -v "\/"`
do 
  cd "${directory2}"
  F2=$(find "${directory2}" -name "$F")
  if [ "$F2" = "" ]
  then
        print "WARNING: Could not locate file : $F in $directory2"
  fi
  cmp  "${directory2}"/"${F}"  "${directory1}"/"${F}"  >/dev/null;REPLY=$?
  if [ ${REPLY} -eq 0 ]
  then
        file_arr_idd[i]="${F}"
	((i+=1))
  else
        file_arr_diff[j]="${F}"
	((j+=1))
  fi
done

for FILES in ${file_arr_idd[*]} 
do
   echo "$FILES are identical" 
done
	
echo "********************************************************************************************"

for FILES in ${file_arr_diff[*]} 
do
   echo "$FILES are different" 
done

echo "************************************End of Program*******************************************"



---------- Post updated at 03:13 PM ---------- Previous update was at 03:13 PM ----------

Thanks a lot for your help and prompt reply Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Please help in populating output in table using shell scripting

Below is my code where i tried using table tag to print out put in table but its not working #!/bin/ksh #] && . ./.profile 2>/dev/null if test -f '.profile'; then . ./.profile; fi; #. .profile LOG_DIR=/app/rpx/jobs/scripts/just/logs sendEmail() { pzCType="$1";... (5 Replies)
Discussion started by: ankit.mca.aaidu
5 Replies

2. Shell Programming and Scripting

Populating a BASH array with a list of files including spaces-in-the-name

For the record, I already tried telling mgmt and the users to disallow spaces in filenames for this script, but it isn't happening for a number of ID10T-error-based reasons. I have simple list of 3 files in a directory that are named like this: bash-3.2$ ls -1 file* file1 file1 part2... (2 Replies)
Discussion started by: ckmehta
2 Replies

3. Programming

Populating Lists in Def using Python

Dipping around in python again and need to create a def that will populate a list(content) with the files that os.walk finds from within this directory and then I will re.search through each files looking for content. In learning Python, can someone point me in the right direction. This is what I... (3 Replies)
Discussion started by: metallica1973
3 Replies

4. UNIX for Dummies Questions & Answers

Help populating double quotes using awk

Want to populate double quotes for each filed using awk: Input: cat file.txt => "1-23-test_test1-test2" Required output : "1-23-test_test1-test2"|"#GT_properties_xyz" Was trying the below command on solaris 9 machine : awk -F"|" '{print $1"|""#GT_properties_xyz"}' file.txt ... (8 Replies)
Discussion started by: rajachandhok
8 Replies

5. Programming

Populating Associate Arrays in PHP

I'm not very good at associative arrays; and working on this PHP code has got me a bit stumped. My goal is to populate a (multidimensional) associative array in a PHP while look after a MySQL query. The code fragment looks like this: while($campaign_row = mysql_fetch_array($campaigninfo)) { ... (9 Replies)
Discussion started by: Neo
9 Replies

6. Programming

populating a JList

Hi, I have to create a JList and the items I need to display are store in HashMap table. What would be the easiest way to populate this JList. Basically the items I want to display/show in the JList are the key values of the HashMap. Thanks in advance for any suggestions. (0 Replies)
Discussion started by: arizah
0 Replies

7. Shell Programming and Scripting

awk - Pre-populating an array from system command output

So, here's a scenario that requires the same logic as what I'm working on: Suppose that you have a directory containing files named after users. For awk's purposes, the filename is a single field-- something parse-friendly, like john_smith. Now, let's say that I'd like to populate an array in... (2 Replies)
Discussion started by: treesloth
2 Replies

8. Shell Programming and Scripting

Populating array raised an error

Hi, The following test case populate an array named: array3. Since array1 and array2 are equal in length and values array3 will remain empty. #!/usr/bin/ksh test() { set -A array1 "A" set -A array2 "A" NUM_1=`echo ${#array1}` print "num elenemt in NUM_1 is ${NUM_1}" i=1 for ELE2 in... (1 Reply)
Discussion started by: yoavbe
1 Replies

9. Shell Programming and Scripting

populating array using awk

Hi. I have a file with the following structer: DB DISK LOCATION SIZE ============================================ PROD DATA_01 /dev/dm-23 10 PROD DATA_02 /dev/dm-24 10 PROD DATA_03 /dev/dm-25 10 DEV DATA_04 /dev/dm-26 10 DEV DATA_05 ... (1 Reply)
Discussion started by: yoavbe
1 Replies

10. Shell Programming and Scripting

Awk help with populating variable

Hi, I'm looking for help trying to parse a data stream. Any help would be greatly appreciated. My awk statement is awk '/Aug/{a=$2}/vol/{print a, host, $1, $2, $3, $4, $5}' out.txt Sample Data Stream "out.txt" ----------------------------- # Aug 3 00:00:00 2008 ===== DF =====... (3 Replies)
Discussion started by: jmd2004
3 Replies
Login or Register to Ask a Question