Operating System - HP-UX
1753428 Members
4780 Online
108793 Solutions
New Discussion юеВ

Re: Newbie Array Question

 
SOLVED
Go to solution
Scott Frye_1
Super Advisor

Newbie Array Question

I want to create two arrays. I've got the following code in place:

cat /tmp/thrdsts.txt | while line=`line`
do
thread=`echo $line | awk '{print $1}'`
status=`echo $line | awk '{print $2}'`
done

I'm hoping this will get the assigning part done(am I right?). Next question is how do I reference these. I've trien echo $thread[1], but all I get is [1].

I'm using what I think is bourne shell /bin/sh, on HP-UX 11.0

Thanks
Scott
6 REPLIES 6
Ralph Grothe
Honored Contributor

Re: Newbie Array Question

Don't know if this helps.

The standard HP-UX Bourne Shell is a Posix shell (see "man sh-posix", especially the section on arrays)

Initilisation and value assignment can take place in the same line.

e.g.

# set -A array tic tac toe

# echo ${array[*]}
tic tac toe

or you can assign elements individually

e.g.

# array[${#array[*]}]=donald

# echo ${array[*]}
tic tac toe donald

n.b. just insert the correct index in the square brackets, the ${#array[*]} construct derefernces the dimension of the array, thus the right index for the next element

Madness, thy name is system administration
John Poff
Honored Contributor
Solution

Re: Newbie Array Question

Hi Scott,

One way to do it in the POSIX shell is like this:

set -A thread $(awk '{print $1} /tmp/thrdsts.txt)
set -A status $(awk '{print $2} /tmp/thrdsts.txt)


echo "thread[1] is ${thread[1]}"


You can use the 'set -A' like above to declare your variable as an array and set the elements of the array to the list provided by the next argument.

In the POSIX shell you are limited to 1024 elements in your array, so if you have a large file you might want to consider using something like Perl.

JP
Michael_356
Frequent Advisor

Re: Newbie Array Question

CNT=0
cat /tmp/thrdsts.txt | while line=`line`
do
thread_$CNT=`echo $line | awk '{print $1}'`
status_$CNT=`echo $line | awk '{print $2}'`
CNT=`expr $CNT + 1`
done
Ralph Grothe
Honored Contributor

Re: Newbie Array Question

Addendum, as for the part of referncing elements:

# i=0; while [ $i -lt ${#array[*]} ];do echo ${array[$i]};((i+=1));done
tic
tac
toe
donald
Madness, thy name is system administration
Fred Ruffet
Honored Contributor

Re: Newbie Array Question

You should use this instead :
let count=1
cat /tmp/thrdsts.txt | while line=`line`
do
thread[$i]=`echo $line | awk '{print $1}'`
status[$i]=`echo $line | awk '{print $2}'`
let i=i+1
done

then you will refer to arrays with :
${thread[$n]}

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Scott Frye_1
Super Advisor

Re: Newbie Array Question

Thank you all very much. Some of these methods I tried and couldn't get to work easily. My favorite and (in my opinion) the cleanest was
set -A thread $(awk '{print $1}' /tmp/thrdsts.txt)
set -A status $(awk '{print $2}' /tmp/thrdsts.txt)

I like it because there is no need for a while.

Thanks to all who replied.