0

I want to print the whole array in tcsh, because then i want to pipe it to uniq, is there a way to do it in tcsh.

i had set array

and was printing it:

echo "$array"

but it ended up with error: Word too long, it works for smaller arrrays,

Is there some simple way to do it?

Ricsie
  • 111

2 Answers2

1

You must have a very old version of tcsh, that Word too long limit was removed in tcsh version 6.14.03 in 2006.

echo "$array"

Would pass the concatenation of the array elements to echo as one element but fail if any contain a newline character.

echo "$array:q"

would be more correct to pass the concatenation of all elements.

To pass all the elements of the array (except the empty ones; though that will be fixed in the next release) as separate arguments to echo, that would be:

echo $array:q

But if echo "$array" fails for you with Word too long, echo in that very old version may also fail with echo: Too many arguments..

0

In tcsh you'd use arrays as follow.

Define the array

set myarray = ( element1 element2 element3 element4 etc )

Output the whole array

printf '%s\n' "$array[-]"

Print element 1 to 3

printf '%s\n' "$array[1-3]"

NOTE: In Bash the first element of the array starts at 0 (zero).

Also add set verbose on top of your script to see what lines are interpreted and where it fails.

  • How would that address the Word too long error? – Keith Thompson Nov 07 '13 at 23:04
  • @KeithThompson making sure he understands how arrays work in *csh shells. – Valentin Bajrami Nov 07 '13 at 23:08
  • Ok, but echo "$array" should work perfectly well to print an entire array -- except for the "Word too long" problem, to which printf '%s\n' "$array[-]" is equally vulnerable. – Keith Thompson Nov 07 '13 at 23:10
  • @KeithThompson except he shows set array only which will result to nothing (empty) output. He probably is overriding the array somehow and resulting in the error. set array; echo $array should return nothing, let alone "Word too long" – Valentin Bajrami Nov 07 '13 at 23:16
  • 1
    Hmm. I assumed that set array was an abbreviation of the actual command he used. If he got "word too long", he's obviously setting it to something. Your recent comment on the question is a good one. – Keith Thompson Nov 07 '13 at 23:25
  • Yes im setting it to quiet a long list of lines, where are few whic hare two or more times and need to sort it and do a uniq – Ricsie Nov 08 '13 at 01:14