Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: (question) C struct-like template to read positional arguments?

On Sun, Jan 17, 2021 at 5:53 PM William Park <opengeometry@xxxxxxxx> wrote:
> In C struct like,
>     struct {
>         char aa[32], bb[32], skip[128], cc[7], dd[32], ...;
>     }
> you just access variables, and compiler will do the offsetting.

I'm having a hard time figuring out what you're talking about here.
How is this structure initialized?  If you're casting a char* to a
pointer to this struct type, I suppose that gets you a bunch of
pointers into the array (unless the compiler does pointer alignment on
word boundaries, in which case dd following that cc[7] is going to be
problematic), but those aren't usable as strings because you don't
have the terminating NUL bytes.  So what do you mean?  And IMO in any
event, that's a horrible programming style.

> Can zsh do something similar?
> [...]
> I know I can do
>     aa=( "${@:1:32}" )
>     bb=( "${@:33:32}" )
>     cc=( "${@:193:7}" )
>     ...
> But, it's so easy to make mistake and very difficult to catch it.

Why do you have 200 positional parameters that have to be split up
into subsets this way?  Seems like it'd be at least as easy to make a
mistake when creating the argument list for the call as when doing the
subsetting.  However ...

>     template=(
>         aa 32
>         bb 32
>         skip 128
>         cc 7
>         dd 32
>         ...
>     )

I think what you're asking for might be

for var len in $template
  set -A $var "${@[1,len]}"
  shift len

Messages sorted by: Reverse Date, Date, Thread, Author