Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

vector generation is a technique which allows us to generate lists without defining iterators and write loops. They are useful in many situations, and can turn code to much more succinct and compact. They also facilitate quick composition of more complex constructs via command expansion.

Here's an example: I have a program which operates on multiple host arguments. I need to generate a list of host names which have a sequential number embedded in them, e.g:

a1z a2z a3z a4z a5z

In bash one can write:

$ progname a{1..5}z

which will expand to:

$ progname a1z a2z a3z a4z a5z

In tcsh, which lacks this useful argument vector-generation feature, we can sometimes use seq. Unfortunately, seq is limited to numbers only. man seq says that seq supports a --format argument, but it doesn't seem to do the really useful thing (allowing arbitrary formats including a number). Instead it only supports changing the floating-point format of a number alone (a pity).

Of course, we can delegate the expansion to bash + echo using both command expansion and quoting of the full bash -c argument:

tcsh> progname `bash -c "echo a{1..5}z"`

Similarly, we could delegate the string vector generation to big utilities like awk, perl, python, lisp, or R using `` (command expansion).

The question is: are there any other simple ways to generate such vectors in tcsh. By 'simple' I mean a small utility like seq, or even better, a built-in shell construct, and without resorting to writing loops or delegating the vector generation to "big" programs like bash/awk/clisp/perl/python/R?

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

You can pipe the output of seq.

progname `seq 5 | sed -e 's/^/a/' -e 's/$/z/'`

You can use history substitution to add a prefix to all the elements of an array:

set a = (`seq 5`)
progname ${a:s//a/}

I don't know how to add a suffix, but I'm not a tcsh expert.


Tcsh is kind of dead this century, and even before was widely recognized as less suitable for scripting than Bourne-style shells. How about using zsh?

a=($(seq 5))
progname a${^a}z
share|improve this answer
    
Thanks Gilles. Just to follow up on the zsh suggestion: I never use tcsh for scripting. file-redirection is painful and inconsistent, flow-control syntax is rough and newline sensitive, but in interactive work I still find it superior in several areas. One example is in its support for alias arguments. See for example: stackoverflow.com/questions/14966102 –  arielf Apr 8 '13 at 23:05
    
@arielf Alias arguments are kinda pointless, you can usually write a function. Your arithmetic alias is relying not just on alias arguments but on the weird way it's implemented through history substitution. –  Gilles Apr 9 '13 at 21:18
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.