zsh-users
 help / color / mirror / Atom feed
* (question) C struct-like template to read positional arguments?
       [not found] <20210118015252.GA3550.ref@node1.localdomain>
@ 2021-01-18  1:52 ` William Park
  2021-01-18 21:39   ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: William Park @ 2021-01-18  1:52 UTC (permalink / raw)
  To: Zsh Users

Hi all,

I have question...

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.

Can zsh do something similar?

That is, you could probably build the template like
    template=(
	aa 32
	bb 32
	skip 128
	cc 7
	dd 32
	...
    )
and read into them, then just use variables, without worrying about
offsets.  So, ${cc[@]} would be equivalent to ${@:193:7}, and ${cc[1]}
would be same as ${194}.

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.

-- 
William Park <opengeometry@yahoo.ca>


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: (question) C struct-like template to read positional arguments?
  2021-01-18  1:52 ` (question) C struct-like template to read positional arguments? William Park
@ 2021-01-18 21:39   ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2021-01-18 21:39 UTC (permalink / raw)
  To: Zsh Users

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
do
  set -A $var "${@[1,len]}"
  shift len
done


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-01-18 21:40 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20210118015252.GA3550.ref@node1.localdomain>
2021-01-18  1:52 ` (question) C struct-like template to read positional arguments? William Park
2021-01-18 21:39   ` Bart Schaefer

zsh-users

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://inbox.vuxu.org/zsh-users

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-users zsh-users/ http://inbox.vuxu.org/zsh-users \
		zsh-users@zsh.org
	public-inbox-index zsh-users

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


code repositories for the project(s) associated with this inbox:

	https://git.vuxu.org/mirror/zsh/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git