zsh-workers
 help / color / mirror / Atom feed
* RFC PATCH: Sketch at :@ subscripting
@ 2020-12-18 13:18 Mikael Magnusson
  2020-12-19  9:13 ` Stephane Chazelas
  0 siblings, 1 reply; 2+ messages in thread
From: Mikael Magnusson @ 2020-12-18 13:18 UTC (permalink / raw)
  To: zsh-workers

As the subject says, this is in a very early idea stage, but it does work for basic cases at least.

The idea is that you can do this:
% typeset -a somearray=( 'data here' 'some words' etc etc 1 2 3 4 ) idx=(1 3 5)
% echo ${somearray:@idx}
data here etc 1

The way it currently works also lets you do this:
% typeset -a somearray=( 'data here' 'some words' etc etc 1 2 3 4 ) idx=( '(r)<->' '(I)etc' )
% echo ${somearray:@idx}
1 4

Not decided on if that's desirable :).

Anyway, the code as it is written now is very hacky and I'm wondering if I missed the existence of some useful functions, or if the stuff I'm using now would need to be refactored a bit to make this actually possible without doing stupid things like zhtricat("[", *sub_it, "]").

Also curious why getindex() unconditionally *writes* '[' to the first character in the input string passed to it...

---
 Src/subst.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)

diff --git a/Src/subst.c b/Src/subst.c
index 8de201b663..943b2546c4 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -3167,6 +3167,67 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
 	    }
 	    break;
 	}
+    } else if (inbrace && (*s == '@')) {
+	/* use every element of the array specified by @ as a subscript */
+	char **idx;
+	++s;
+	if (*itype_end(s, IIDENT, 0)) {
+	    untokenize(s);
+	    zerr("not an identifier: %s", s);
+	    return NULL;
+	}
+	if (vunset) {
+	    if (vunset > 0 && unset(UNSET)) {
+		*idend = '\0';
+		zerr("%s: parameter not set", idbeg);
+		return NULL;
+	    }
+	    isarr = 0;
+	    val = dupstring("");
+	} else {
+	    char *sval;
+	    idx = getaparam(s);
+	    if (!idx) {
+		sval = getsparam(s);
+		if (sval)
+		    idx = hmkarray(sval);
+	    }
+	    if (idx) {
+		if (isarr) {
+		    if (PM_TYPE(vbuf.pm->node.flags) & PM_ARRAY) {
+			/* actual code is here */
+			LinkList list = newlinklist();
+			char **sub_it, **val_it;
+			for (sub_it = idx; *sub_it; sub_it++) {
+			    char *tmp = zhtricat("[", *sub_it, "]");
+			    /* getindex() modifies vbuf in ways that break further
+			     * subscripting with differing flags, so better not to
+			     * touch it at all. */
+			    struct value vbuf_copy = vbuf;
+			    getindex(&tmp, &vbuf_copy, 0);
+			    for (val_it = getarrvalue(&vbuf_copy); *val_it; val_it++) {
+				addlinknode(list, *val_it);
+			    }
+			}
+			aval = hlinklist2array(list, !copied);
+			copied = 1;
+		    } else {
+			zerr("assoc @ not implemented");
+		    }
+		} else {
+		    zerr("scalar @ not implemented");
+		}
+	    } else {
+
+		if (unset(UNSET)) {
+		    zerr("%s: parameter not set", s);
+		    return NULL;
+		}
+		isarr = 0;
+		val = dupstring("");
+	    }
+	}
     } else if (inbrace && (*s == '^' || *s == Hat)) {
 	char **zip;
 	int shortest = 1;
-- 
2.15.1



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

* Re: RFC PATCH: Sketch at :@ subscripting
  2020-12-18 13:18 RFC PATCH: Sketch at :@ subscripting Mikael Magnusson
@ 2020-12-19  9:13 ` Stephane Chazelas
  0 siblings, 0 replies; 2+ messages in thread
From: Stephane Chazelas @ 2020-12-19  9:13 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: zsh-workers

2020-12-18 14:18:15 +0100, Mikael Magnusson:
[...]
> The idea is that you can do this:
> % typeset -a somearray=( 'data here' 'some words' etc etc 1 2 3 4 ) idx=(1 3 5)
> % echo ${somearray:@idx}
> data here etc 1
[...]

Hi Mikael,

I can't help but think that allowing to specify the indexes
directly as perl does for instance in:

print @list[1, 4, 2, 7..10, @idx, -1];

And in assignments as well:

@list[@idx] = qw(new values here);

would be more useful.

It's unfortunate that "," is used for ranges in $a[1,3] in zsh
especially considering that "," is also an arithmetic operator
(btw:
  $ a=({a..z})
  $ echo ${a[1,3]}
  a b c
  $ i=1,3
  $ echo ${a[i]}
  c
  $ echo ${a[$i]}
  a
??) and that {1,3,{5..7}} otherwise follows the perl semantics
(and extends it as it allows backward ranges).

${list[2 6 8]} in zsh wouldn't work as ${list[2 -1]} already
means something different, but maybe ${list[2;6,9;${(j[;])idx},-1]}
would?

That would mean no backward range as $a[3,1] ATM is the empty
list but backward sets would still be possible with $a[3;2;1].

Maybe another option could be $a[{1,4,{8..2},$^idx}] so as to
reuse an already known syntax?

Except that:

$ i=(a b)
$ echo {1,2,$^i}.
1. 2. a. 1. 2. b.
$ echo {1,2,{a,b}}.
1. 2. a. b.

Syntax would also have to be restricted to a[{...}], no
a[{1,2}0] for instance.

a[{${(f)^"$(shuf -n 5 -i 1-100)"}]=({a..e})
soon becomes awkward as well.

There's also the question of

a[1;5]=(a b c)
a[1;5]=(a)
...

Having a syntax that could also apply to associative arrays
would be best.

-- 
Stephane


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

end of thread, other threads:[~2020-12-19  9:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-18 13:18 RFC PATCH: Sketch at :@ subscripting Mikael Magnusson
2020-12-19  9:13 ` Stephane Chazelas

zsh-workers

This inbox may be cloned and mirrored by anyone:

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

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

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


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