zsh-users
 help / color / Atom feed
* (0) not working ?
@ 2019-11-27 13:00 Marc Chantreux
  2019-11-27 13:54 ` Perry Smith
  2019-11-27 14:40 ` (0) not working ? Marc Chantreux
  0 siblings, 2 replies; 11+ messages in thread
From: Marc Chantreux @ 2019-11-27 13:00 UTC (permalink / raw)
  To: Zsh Users

hello people,

in zshexpn, i read about 0: "This is a shorthand for `ps:\0:'".
so i wrote:

    assigned_to=( qa c c++ )
    local -A user=(
        login marc
        roles ${(j:\0:)assigned_to}
    )
    print -l ${(s:\0:)user[roles]}
    print -l ${(@0)user[roles]}
    print -l ${(@0)user[roles]}

and i got

    qa
    c
    c++
    qa\0c\0c++
    qa\0c\0c++

is there something to setopt? did i miss something ?

regards,
marc

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

* Re: (0) not working ?
  2019-11-27 13:00 (0) not working ? Marc Chantreux
@ 2019-11-27 13:54 ` Perry Smith
  2019-11-27 14:18   ` Marc Chantreux
  2019-11-27 14:40 ` (0) not working ? Marc Chantreux
  1 sibling, 1 reply; 11+ messages in thread
From: Perry Smith @ 2019-11-27 13:54 UTC (permalink / raw)
  To: Marc Chantreux; +Cc: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 883 bytes --]

I’m very new here so take what I say with caution but why
did you put the backslash before the 0?

Also, this is going to null terminate each word and so what I
would do to verify this is either pipe it into xargs with -0 (if you
have that) or od -cx so you can verify that it is working like
you expect.

> On Nov 27, 2019, at 7:00 AM, Marc Chantreux <eiro@phear.org> wrote:
> 
> hello people,
> 
> in zshexpn, i read about 0: "This is a shorthand for `ps:\0:'".
> so i wrote:
> 
>    assigned_to=( qa c c++ )
>    local -A user=(
>        login marc
>        roles ${(j:\0:)assigned_to}
>    )
>    print -l ${(s:\0:)user[roles]}
>    print -l ${(@0)user[roles]}
>    print -l ${(@0)user[roles]}
> 
> and i got
> 
>    qa
>    c
>    c++
>    qacc++
>    qacc++
> 
> is there something to setopt? did i miss something ?
> 
> regards,
> marc


[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 2966 bytes --]

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

* Re: (0) not working ?
  2019-11-27 13:54 ` Perry Smith
@ 2019-11-27 14:18   ` Marc Chantreux
  2019-11-27 14:56     ` Perry Smith
  0 siblings, 1 reply; 11+ messages in thread
From: Marc Chantreux @ 2019-11-27 14:18 UTC (permalink / raw)
  To: Perry Smith; +Cc: Marc Chantreux, Zsh Users

hello Perry,

> I’m very new here so take what I say with caution but why
> did you put the backslash before the 0?

if you use xxd:

    echo -n 0 $'\0'|xxd

you can see that 0 is the symbol 0x30 (48th of the ascii table)
when \0 is 00.

\0 is non-sense in a legit text stream so it can be used as
a separator instead of all those separators that can exist in
text ( "\n", ",", ":" " ", "\t").

see xargs -0, find -print0, ...

regards
marc



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

* Re: (0) not working ?
  2019-11-27 13:00 (0) not working ? Marc Chantreux
  2019-11-27 13:54 ` Perry Smith
@ 2019-11-27 14:40 ` Marc Chantreux
  1 sibling, 0 replies; 11+ messages in thread
From: Marc Chantreux @ 2019-11-27 14:40 UTC (permalink / raw)
  To: Zsh Users

hello,

I spotted the error thanks to an example in a private message:

Ok. i got it reading your example: (pj:\0:) and (j:\0:) don't
behave the same even if it looks so when you print the string.

    print $user[roles]
    print -l ${(0)user[roles]}

gives

    qa=00c=00c++
    qa=00c=00c++

when serialized with ${(j:\0:)assigned_to} and

    qa=00c=00c++
    qa
    c
    c++

when serialized with ${(pj:\0:)assigned_to}.

thanks a lot and regards,
marc

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

* Re: (0) not working ?
  2019-11-27 14:18   ` Marc Chantreux
@ 2019-11-27 14:56     ` Perry Smith
  2019-11-27 16:15       ` (0) works as a charm Marc Chantreux
  0 siblings, 1 reply; 11+ messages in thread
From: Perry Smith @ 2019-11-27 14:56 UTC (permalink / raw)
  To: Marc Chantreux; +Cc: Zsh Users

[-- Attachment #1.1: Type: text/plain, Size: 1304 bytes --]

It appears that you need the “p” flag for the join and probably for the split as well.

Also, @ says “In  double  quotes,  array  elements  are put into separate words.”
But you don’t have double quotes so the @ isn’t needed.

> % var1=( a b c d )   
> % var2=${(j:\0:)var1}
> % print $#var2
> 10

At this point var2 is a scaler with two characters ‘\’ and ‘0’ separating what were the words.

> % var3=${(pj:\0:)var1}
> % print $#var3
> 7

This is what you are expecting / needing

> % print -l ${(0)var3}
> a
> b
> c
> d

The print works

> % print -l ${(@0)var3}
> a
> b
> c
> d


The @ isn’t needed

(Again… I’m new here so please verify everything)

> On Nov 27, 2019, at 8:18 AM, Marc Chantreux <eiro@phear.org> wrote:
> 
> hello Perry,
> 
>> I’m very new here so take what I say with caution but why
>> did you put the backslash before the 0?
> 
> if you use xxd:
> 
>    echo -n 0 $'\0'|xxd
> 
> you can see that 0 is the symbol 0x30 (48th of the ascii table)
> when \0 is 00.
> 
> \0 is non-sense in a legit text stream so it can be used as
> a separator instead of all those separators that can exist in
> text ( "\n", ",", ":" " ", "\t").
> 
> see xargs -0, find -print0, ...
> 
> regards
> marc
> 
> 


[-- Attachment #1.2: Type: text/html, Size: 6719 bytes --]

<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">It appears that you need the “p” flag for the join and probably for the split as well.<div class=""><br class=""></div><div class="">Also, @ says “In &nbsp;double &nbsp;quotes, &nbsp;array &nbsp;elements &nbsp;are put into separate words.”</div><div class="">But you don’t have double quotes so the @ isn’t needed.</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(217, 251, 233);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">% var1=( a b c d )&nbsp; &nbsp;</span></div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(217, 251, 233);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">% var2=${(j:\0:)var1}</span></div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(217, 251, 233);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">% print $#var2</span></div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(217, 251, 233);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">10</span></div></blockquote><div class=""><br class=""></div>At this point var2 is a scaler with two characters ‘\’ and ‘0’ separating what were the words.</div><div class=""><br class=""><blockquote type="cite" class=""><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(217, 251, 233);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">% var3=${(pj:\0:)var1}</span></div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(217, 251, 233);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">% print $#var3</span></div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(217, 251, 233);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">7</span></div></blockquote><div class=""><br class=""></div>This is what you are expecting / needing</div><div class=""><br class=""><blockquote type="cite" class=""><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(217, 251, 233);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">% print -l ${(0)var3}</span></div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(217, 251, 233);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">a</span></div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(217, 251, 233);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">b</span></div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(217, 251, 233);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">c</span></div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(217, 251, 233);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">d</span></div></blockquote><div class=""><br class=""></div>The print works</div><div class=""><br class=""><blockquote type="cite" class=""><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(217, 251, 233);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">% print -l ${(@0)var3}</span></div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(217, 251, 233);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">a</span></div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(217, 251, 233);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">b</span></div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(217, 251, 233);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">c</span></div><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(217, 251, 233);" class=""><span style="font-variant-ligatures: no-common-ligatures" class="">d</span></div></blockquote><div class=""><div style="margin: 0px; font-stretch: normal; font-size: 14px; line-height: normal; font-family: Menlo; background-color: rgb(217, 251, 233);" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><br class=""></span></div></div><div><br class=""></div><div>The @ isn’t needed</div><div><br class=""></div><div>(Again… I’m new here so please verify everything)</div><div><br class=""><blockquote type="cite" class=""><div class="">On Nov 27, 2019, at 8:18 AM, Marc Chantreux &lt;<a href="mailto:eiro@phear.org" class="">eiro@phear.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">hello Perry,<br class=""><br class=""><blockquote type="cite" class="">I’m very new here so take what I say with caution but why<br class="">did you put the backslash before the 0?<br class=""></blockquote><br class="">if you use xxd:<br class=""><br class=""> &nbsp;&nbsp;&nbsp;echo -n 0 $'\0'|xxd<br class=""><br class="">you can see that 0 is the symbol 0x30 (48th of the ascii table)<br class="">when \0 is 00.<br class=""><br class="">\0 is non-sense in a legit text stream so it can be used as<br class="">a separator instead of all those separators that can exist in<br class="">text ( "\n", ",", ":" " ", "\t").<br class=""><br class="">see xargs -0, find -print0, ...<br class=""><br class="">regards<br class="">marc<br class=""><br class=""><br class=""></div></div></blockquote></div><br class=""></div></body></html>

[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 2966 bytes --]

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

* (0) works as a charm
  2019-11-27 14:56     ` Perry Smith
@ 2019-11-27 16:15       ` Marc Chantreux
  2019-11-27 16:24         ` Roman Perepelitsa
  0 siblings, 1 reply; 11+ messages in thread
From: Marc Chantreux @ 2019-11-27 16:15 UTC (permalink / raw)
  To: Perry Smith; +Cc: Marc Chantreux, Zsh Users

hello

reconsidering the example i got privately:

print has this -r flags that makes things appears:

    foo=( this bar )
    a=${(pj:\0:)foo}
    b=${(j:\0:)foo}
    print -lr $a $b

gives

    this\0bar
    this\0bar

that's why (p) is mandatory while joining:
* if p is present while joining, it must be present while splitting
* -1s/present/absent/g
* as the manual says: 0 means ps:\0:

regards
marc

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

* Re: (0) works as a charm
  2019-11-27 16:15       ` (0) works as a charm Marc Chantreux
@ 2019-11-27 16:24         ` Roman Perepelitsa
  2019-11-27 18:14           ` Perry Smith
  0 siblings, 1 reply; 11+ messages in thread
From: Roman Perepelitsa @ 2019-11-27 16:24 UTC (permalink / raw)
  To: Marc Chantreux; +Cc: Perry Smith, Zsh Users

[-- Attachment #1: Type: text/plain, Size: 212 bytes --]

> * if p is present while joining, it must be present while splitting
> * -1s/present/absent/g
> * as the manual says: 0 means ps:\0:


p simply converts \0 to ascii 0. It's the same as '\0' vs $'\0'.

Roman.

>

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

* Re: (0) works as a charm
  2019-11-27 16:24         ` Roman Perepelitsa
@ 2019-11-27 18:14           ` Perry Smith
  2019-11-27 19:33             ` (about quoting) " Marc Chantreux
  0 siblings, 1 reply; 11+ messages in thread
From: Perry Smith @ 2019-11-27 18:14 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 1110 bytes --]

On Nov 27, 2019, at 10:24 AM, Roman Perepelitsa <roman.perepelitsa@gmail.com> wrote:
> 
>> * if p is present while joining, it must be present while splitting
>> * -1s/present/absent/g
>> * as the manual says: 0 means ps:\0:
> 
> p simply converts \0 to ascii 0. It's the same as '\0' vs $'\0’.

I’m still not clear…

> QUOTING
>        A character may be quoted (that is, made to stand for itself) by preceding it with a `\'.  `\'  followed  by  a
>        newline is ignored.
> 
>        A  string enclosed between `$'' and `'' is processed the same way as the string arguments of the print builtin,
>        and the resulting string is considered to be entirely quoted.  A literal `'' character can be included  in  the
>        string by using the `\'' escape.
> 
>        All  characters enclosed between a pair of single quotes ('') that is not preceded by a `$' are quoted.

Are you saying that ‘\0’ is a quoted null character while $’\0’
is a quoted string with one character which is a null and somehow
zsh keeps quoted characters different from quoted strings?


[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 2966 bytes --]

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

* (about quoting) Re: (0) works as a charm
  2019-11-27 18:14           ` Perry Smith
@ 2019-11-27 19:33             ` " Marc Chantreux
  2019-11-27 20:21               ` Perry Smith
  0 siblings, 1 reply; 11+ messages in thread
From: Marc Chantreux @ 2019-11-27 19:33 UTC (permalink / raw)
  To: Perry Smith; +Cc: Zsh Users

hello,

> Are you saying that ‘\0’ is a quoted null character while $’\0’
> is a quoted string with one character which is a null and somehow
> zsh keeps quoted characters different from quoted strings?

the opposite: $'' is yet another quoting style in zsh to know what
will be expanded

       | ""    $''  ''  none
    ---+-----------------
    $  | yes   no   no  yes
    \  | yes   yes  no  no[1]
    
    [1] remove the '\' symbol

    print -r "\n" $'\n' '\n' \n | xxd

first i was confused because by default, print interpolate \
before printing as long as you don't use -r.

so

    x='\0'
    print $x
    x=$'\0'
    print $x

looks the same

regards
marc

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

* Re: (about quoting) Re: (0) works as a charm
  2019-11-27 19:33             ` (about quoting) " Marc Chantreux
@ 2019-11-27 20:21               ` Perry Smith
  2019-11-28 10:55                 ` Marc Chantreux
  0 siblings, 1 reply; 11+ messages in thread
From: Perry Smith @ 2019-11-27 20:21 UTC (permalink / raw)
  To: Zsh Users

[-- Attachment #1: Type: text/plain, Size: 976 bytes --]



> On Nov 27, 2019, at 1:33 PM, Marc Chantreux <eiro@phear.org> wrote:
> 
> hello,
> 
>> Are you saying that ‘\0’ is a quoted null character while $’\0’
>> is a quoted string with one character which is a null and somehow
>> zsh keeps quoted characters different from quoted strings?
> 
> the opposite: $'' is yet another quoting style in zsh to know what
> will be expanded
> 
>       | ""    $''  ''  none
>    ---+-----------------
>    $  | yes   no   no  yes
>    \  | yes   yes  no  no[1]
> 
>    [1] remove the '\' symbol
> 
>    print -r "\n" $'\n' '\n' \n | xxd
> 
> first i was confused because by default, print interpolate \
> before printing as long as you don't use -r.
> 
> so
> 
>    x='\0'
>    print $x
>    x=$'\0'
>    print $x
> 
> looks the same

AH!!  So… testing with “print” (without -r) has pitfalls…

I had no idea print was interpreting things on output…

thank you all for your patience


[-- Attachment #2: smime.p7s --]
[-- Type: application/pkcs7-signature, Size: 2966 bytes --]

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

* Re: (about quoting) Re: (0) works as a charm
  2019-11-27 20:21               ` Perry Smith
@ 2019-11-28 10:55                 ` Marc Chantreux
  0 siblings, 0 replies; 11+ messages in thread
From: Marc Chantreux @ 2019-11-28 10:55 UTC (permalink / raw)
  To: Perry Smith; +Cc: Zsh Users

> > so
> >    x='\0'
> >    print $x
> >    x=$'\0'
> >    print $x
> > looks the same
> AH!!  So… testing with “print” (without -r) has pitfalls…

not really pitfalls as the default behaviour probably works
the way we expect most of the time.

> I had no idea print was interpreting things on output…

not "on ouput" but "before releasing on the ouput" but yes: i missed
that point too.

regards
marc

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

end of thread, back to index

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-27 13:00 (0) not working ? Marc Chantreux
2019-11-27 13:54 ` Perry Smith
2019-11-27 14:18   ` Marc Chantreux
2019-11-27 14:56     ` Perry Smith
2019-11-27 16:15       ` (0) works as a charm Marc Chantreux
2019-11-27 16:24         ` Roman Perepelitsa
2019-11-27 18:14           ` Perry Smith
2019-11-27 19:33             ` (about quoting) " Marc Chantreux
2019-11-27 20:21               ` Perry Smith
2019-11-28 10:55                 ` Marc Chantreux
2019-11-27 14:40 ` (0) not working ? Marc Chantreux

zsh-users

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

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.users


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