Re: double "triangle" loop for $* ?
Chris F.A. Johnson wrote:
> On 2006-11-01, Yakov wrote:
> > I'm curious how to write the double "triangle" loop for $* in posix sh
> > analogous to this loop: for(k=0; k<N;k++) for(j=0; j<k; j++) body;
> > preferably in posix shell.
> > I'm experienced shell scripter but here,
> > I'm stumped: for x in $*; do for y in ????; do body; done; done;
> > (???? must become the slice of $* list from $1 to one before
> > the element represented by $x)
>
> Since you are using $* (instead of "$@", which is probably what you
> really want) that is straightforward:
>
> z=
> for x in $*
> do
> z="$z $x"
> for y in $z
> do
> : body
> done
> done
This is nice, thanx, just what I was looking for.
> To use "$@" is more complicated in a POSIX shell, but
> straightforward in bash:
>
> unset z
> for x in $*
> do
> z[${#z[@]}]=$x
> for y in "${z[@]}"
> do
> : body
> done
> done
Nice, too.
Yakov
|