Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

2D Array Creation Question

14 views
Skip to first unread message

Matthew Kruer

unread,
May 31, 2012, 6:37:07 PM5/31/12
to
Is there a better way to create this array?

I know I can do it like this
# Array [Name, Sufix, Fullname]
name=('workflow','application','process','adaptor','event','io')
sufix=('war','jar','jar','jar','jar','jar')
fullname('','','','','','','')

sudo update of array with additional information
for (a=0;a<$name[@];a++)
fullname[a] = `ls -t $name[a]*$sufix[a] | head -1`
done

Ideally I would like it to be like this as it is easier to read.
# Array [name, sufix, fullname]
'workflow', 'war', ''
'application', 'jar', ''
'process', 'jar', ''
'adaptor', 'jar', ''
'event', 'jar', ''
'io', 'jar', ''

Thanks

Dave Gibson

unread,
Jun 2, 2012, 10:53:47 AM6/2/12
to
names=(
workflow war ''
application jar ''
process jar ''
adaptor jar ''
event jar ''
io jar ''
)

# number of elements considered to form a record
recsize=3

# offsets (within a record) to name, extension and fullname
nameoff=0
extoff=1
fulloff=2

for (( n = nameoff, e = extoff, f = fulloff ; n < ${#names[@]} ;
n += recsize, e += recsize, f += recsize )) ; do
read names[$f] <<< \
"$( ls -t -- "${names[$n]}"*"${names[$e]}" 2> /dev/null )"
done

Ed Morton

unread,
Jun 4, 2012, 12:12:28 PM6/4/12
to
Here's an alternative approach to your problem:

ls -rt | gawk '
BEGIN {
name2sfx["workflow"] = "war"
name2sfx["application"] = "jar"
name2sfx["process"] = "jar"
name2sfx["adaptor"] = "jar"
name2sfx["event"] = "jar"
name2sfx["io"] = "jar"
}

{
for (name in name2sfx) {
sfx = name2sfx[name]
if ( ($0 ~ "^" name ".*" sfx "$") && (!done[name,sfx]++) ) {
print name, sfx, $0
}
}
}
'

Might make it a bit easier to do whatever it is you eventually want to do
with the file names.

Regards,

Ed.

Posted using www.webuse.net

Matthew Kruer

unread,
Jun 4, 2012, 3:39:31 PM6/4/12
to
Thanks to the replies. I think I came up with a more elegant solution which was to

NAME=( 'com.application-.*.jar'
'com.process.runtime_.*.jar'
'com.adaptor_.*.jar'
'com.event_.*.jar'
'com.io_.*.jar'
'com.message_.*.jar'
'com.process_.*.jar'
'com.queue_.*.jar'
'com.resource_.*.jar'
'com.search_.*.jar'
'com.service_.*.jar')
I build the array with build in regular expressions. This works better as it’s a single array, and I can use the array regular expressions logic both for the search and the replacement string.

for i in "${!NAME[@]}"
do
FULLNAME[$i]=`ls -t1 $SOURCE | grep ${NAME[$i]} | head -n1`
DATETIME[$i]=`ls -tla1 $SOURCE | grep ${NAME[$i]} | head -n1 | awk '{print $6, $7}'`
done

(yeah I know i could have used awk, but this works and is easier for me to understand being an awk novice. )

for i in "${!FULLNAME[@]}"
do
find $PROJECTDIR -name "file1.xml" -o -name "file2.xml" | xargs sed -i "s|${NAME[$i]}|${FULLNAME[$i]}|g"
done

This seems to work perfectly.

Thanks again.
0 new messages