Array size in C shell scripting

Hi,

I would like to know how to define the size of the array in c shell scripting.

set x=(40 10 30 20)
echo "${#x}"
4

Compare with bash:

x=(40 10 30 20)
echo "${#x[@]}"

In bash ${#x} is like ${#x[0]} i.e. the (string-)length of the first element.

Is it possible to define the size of the array instead of directly giving the input?

Like x is the array variable and it has the size of 10. So that I could pass the input.

The c-shell is very primitive (and buggy).
You cannot declare an array. You have to use the =( ) syntax to extend it.
Clear an array: set x=()
Append an element: set x=($x:q "last")
Prepend an element set x=("first" $x:q)
Change an existing element: set x[1]="new"

2 Likes

Hi,
Thank you.

--- Post updated at 01:41 PM ---

Hi,
Thank you.

Is there any pdf document to learn about c shell scripting? And one more thing is there any way to open the text document using shell scripting?

I cannot recommend the C-shell at all.
My own experience is, it is sufficient for the command line, but it is full of bad surprises if you are going to write longer scripts.
The Internet is full of warnings:
Why you should not use C shell by Chris F.A. Johnson
Csh Programming Considered Harmful
https://www.grymoire.com/Unix/CshTop10.txt

Hi.

I am with MadeInGermany and others in trying to discourage use of csh/tcsh for scripting.

However, if you must use it, see the (old) references below. Search on Google, at publisher websites for possible PDFs, and at Amazon, the latter of which may have new copies of the books, but I'd try for a used copy there as well.

Best wishes ... cheers, drl

Title: The UNIX(tm) C Shell Field Guide
Author: Anderson, G, Anderson, P
Date: 1986
Publisher: Prentice-Hall
ISBN: 0-13-937468-X
Pages: 370
Categories: csh, shell, scripting

Title: Using CSH & Tcsh 
Author: Paul DuBois 
Edition: 1
Date: July 1, 1995
Publisher: O'Reilly Media
ISBN: 1565921321
Pages: 242
Categories: csh, shell, scripting

Title: UNIX(R) Shells by Example 
Subtitle: ... guide to the C, Bourne, and Korn Shells plus Awk, Sed, and Grep
Author: Ellie Quigley
Edition: 4th
Date: 2004
Publisher: Prentice-Hall
ISBN: 013147572X
Pages: 1200
Categories: sh, csh, ksh, grep, sed, awk, scripting, shell, programming
Comments: 4.5 stars, 45 reviews Amazon (2007.07)
Comments: ( I have 2nd Ed, 1997 )
Comments: ( 4th edition includes bash and tcsh chapters )

You could try this:

set ar=()
set x=10
foreach i ( `seq $x`)
   set ar=($ar:q " ")
end
echo ${#ar}

However with my csh (tchs 6.22.01) using $arr:q to print array elements with quotes around them fails to print array elements that are blank. I had to append a single space instead.

Thank you all for the reply.

@Chubler_XL

I need to give input as an array value. Is that possible to do like that

Not quite sure what you mean by input as an array value. It's fairly straight forward to pre-load an array with fixed values or copy an existing array into another:

$ set ar=(10 165 22 156)
$ set ac=( $ar:q )
$ echo ${ar[3])
22
$ echo ${ac[4]}
156

I mean, the array size should automatically increase. ie., consider the array size as 5 initially, then if the input is in size of 10 data(1,2,3,4,5,6,7,8,9,10), then the array size should increase from 5 to 10.

Assigning the array with set will automatically size to the input eg.

$ set arr=(1 2 3 4 5)
$ set newarr=(1 2 3 4 5 6 7 8 9 10)
$ echo ${#arr}
5
$ set arr=( $newarr:q )
$  echo ${#arr}
10

Hi,

In the below code, data is copied from one array variable to another array variable. is it possible to copy a data from a variable (which is not an array) to a variable which is an array?

set ARRAY = ( a b c d e f)

set master_array =
set i = 0

while ( $i < 4 )

set master_array = ( $master_array $ARRAY[$i] )
@ i++
end

Say you have four values in variable VALS like this a b c d then you can build an array containing these with a simple set statement.

$ set VALS="a b c d"
$ set master_array = ( $VALS )
$ echo ${#master_array}
4

or if you have 4 separate variables A1 thru A4 you could use eval like this:

set A1=a
set A2=b
set A3=c
set A4=d

set master_array =
set i = 1

while ( $i < 5 )
  set master_array = ( $master_array:q `eval echo \$A$i` )
  @ i++
end