Re: Newbie Array Question (340 Views)
Reply
Super Advisor
Scott Frye_1
Posts: 272
Registered: ‎05-14-2004
Message 1 of 7 (340 Views)
Accepted Solution

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
Honored Contributor
Ralph Grothe
Posts: 2,631
Registered: ‎04-09-2000
Message 2 of 7 (340 Views)

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
Honored Contributor
John Poff
Posts: 2,448
Registered: ‎05-22-2001
Message 3 of 7 (340 Views)

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
Frequent Advisor
Michael_356
Posts: 69
Registered: ‎09-21-2003
Message 4 of 7 (340 Views)

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
Honored Contributor
Ralph Grothe
Posts: 2,631
Registered: ‎04-09-2000
Message 5 of 7 (340 Views)

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
Honored Contributor
Fred Ruffet
Posts: 1,366
Registered: ‎08-17-2003
Message 6 of 7 (340 Views)

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.)
Super Advisor
Scott Frye_1
Posts: 272
Registered: ‎05-14-2004
Message 7 of 7 (340 Views)

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.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.