zsh-users
 help / color / mirror / code / Atom feed
* ksX-Mailer: MH-E 8.6; GNU Mailutils 3.2; GNU Emacs 24.5.1
@ 2017-09-09  0:00 jdh
  2017-09-10  1:51 ` Phil Pennock
  0 siblings, 1 reply; 3+ messages in thread
From: jdh @ 2017-09-09  0:00 UTC (permalink / raw)
  To: zsh-users



I wanted to use the array flage (i) to get an index into a charagcter array (a string), but am getting incorrect results.  Here is an example code snippet which shows the  oddity.

#
#  below define a table (string) with 4 characters.
chrtab="#()*"
for (( ndx=1;  ndx<=$#chrtab ; ndx++ ))
    do echo "Character is $chrtab[ndx], but retrieved index is $chrtab[(i)$chrtab[ndx]]"
    done

I expect the result to be 1, 2, 3, 4
                 but get  5, 5, 5, 1

Is this a a feature or a bug?



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

* Re: ksX-Mailer: MH-E 8.6; GNU Mailutils 3.2; GNU Emacs 24.5.1
  2017-09-09  0:00 ksX-Mailer: MH-E 8.6; GNU Mailutils 3.2; GNU Emacs 24.5.1 jdh
@ 2017-09-10  1:51 ` Phil Pennock
  2017-09-10  5:08   ` Bart Schaefer
  0 siblings, 1 reply; 3+ messages in thread
From: Phil Pennock @ 2017-09-10  1:51 UTC (permalink / raw)
  To: jdh; +Cc: zsh-users

On 2017-09-09 at 17:00 +1700, jdh wrote:
> I wanted to use the array flage (i) to get an index into a charagcter array (a string), but am getting incorrect results.  Here is an example code snippet which shows the  oddity.
> 
> #
> #  below define a table (string) with 4 characters.
> chrtab="#()*"
> for (( ndx=1;  ndx<=$#chrtab ; ndx++ ))
>     do echo "Character is $chrtab[ndx], but retrieved index is $chrtab[(i)$chrtab[ndx]]"
>     done
> 
> I expect the result to be 1, 2, 3, 4
>                  but get  5, 5, 5, 1
> 
> Is this a a feature or a bug?

Both, I think?

The `i` is documented to return one past the length of the item, so `5`
means "not found".  You can use `I` and see `0` instead.

It's because those are pattern characters, thus the `*` matching the
first item and returning `1`.  So I'd _expect_ that using the `e` flag
too would resolve it, but it only fixes two of those.

    for (( ndx=1;  ndx<=$#chrtab ; ndx++ )); do
      echo "Character is $chrtab[ndx], but retrieved index is $chrtab[(ie)$chrtab[ndx]]"
    done

Result: 5, 5, 3, 4

What works instead is to backslash-escape the metacharacters:

    for (( ndx=1;  ndx<=$#chrtab ; ndx++ )); do
      echo "Character is $chrtab[ndx], but retrieved index is $chrtab[(i)${(q)chrtab[ndx]}]"
    done

Result: 1, 2, 3, 4

I'm not off-hand seeing why `(ie)` indexing wouldn't handle the `#` and
`(` entries though, which is why I think perhaps a bug.  But I'm
probably wrong and I too would be interested in learning why this is
expected behavior.  :)

-Phil


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

* Re: ksX-Mailer: MH-E 8.6; GNU Mailutils 3.2; GNU Emacs 24.5.1
  2017-09-10  1:51 ` Phil Pennock
@ 2017-09-10  5:08   ` Bart Schaefer
  0 siblings, 0 replies; 3+ messages in thread
From: Bart Schaefer @ 2017-09-10  5:08 UTC (permalink / raw)
  To: zsh-users

On Sep 9,  9:51pm, Phil Pennock wrote:
}
} On 2017-09-09 at 17:00 +1700, jdh wrote:
} > Is this a a feature or a bug?
} 
} Both, I think?

Well, not exactly.

} It's because those are pattern characters, thus the `*` matching the
} first item and returning `1`.  So I'd _expect_ that using the `e` flag
} too would resolve it, but it only fixes two of those.

Hrm.  The doc explicitly says:

  ... To match the value
  of a parameter literally in a reverse subscript, rather than as a
  pattern, use `${(q)NAME}' (*Note Parameter Expansion::) to quote the
  expanded value.

However:  All that the (e) flag actually does is untokenize() the
string that was previously tokenized for the (i) flag.  The string
is still thereafter passed through patcompile() and matched as a
pattern against the value.

Interestingly,

torch% print $chrtab[(i)#]    
1
torch% print $chrtab[(ie)#]
5

I don't know why a pattern consisting of an UNtokenized "#" compiles
into something that _doesn't_ match a literal "#".


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

end of thread, other threads:[~2017-09-10  5:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-09-09  0:00 ksX-Mailer: MH-E 8.6; GNU Mailutils 3.2; GNU Emacs 24.5.1 jdh
2017-09-10  1:51 ` Phil Pennock
2017-09-10  5:08   ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

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

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).