help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: Re: (question) C struct-like template to read positional arguments?
Date: Mon, 18 Jan 2021 13:39:36 -0800	[thread overview]
Message-ID: <CAH+w=7aynUKU0OpLbaCjhG3phLdu+xbyHJE3Js2r6SNUQdh2iw@mail.gmail.com> (raw)
In-Reply-To: <20210118015252.GA3550@node1.localdomain>

On Sun, Jan 17, 2021 at 5:53 PM William Park <opengeometry@yahoo.ca> 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

      reply	other threads:[~2021-01-18 21:40 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20210118015252.GA3550.ref@node1.localdomain>
2021-01-18  1:52 ` William Park
2021-01-18 21:39   ` Bart Schaefer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7aynUKU0OpLbaCjhG3phLdu+xbyHJE3Js2r6SNUQdh2iw@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@zsh.org \


* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox


This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).