zsh-users
 help / color / mirror / code / Atom feed
* destructuring assignment
@ 2014-02-03 17:36 Roman Neuhauser
  2014-02-03 18:00 ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Roman Neuhauser @ 2014-02-03 17:36 UTC (permalink / raw)
  To: zsh-users

hello,

having an array such as

  declare -a partitions; partitions=(
    /boot:boot:512:fat:boot,legacy_boot
    :swap:10240:swap
    /:root:-:btrfs
  )

i'd love to iterate it like:

  for mountp name size fs flags in ...; do
    # 1st iteration
    # $mountp=/boot $name=boot $size=512 fs=fat flags=boot,legacy_boot
    # 2nd iteration
    # $mountp= $name=swap $size=10240 fs=swap flags=
    # 3rd iteration
    # $mountp=/ $name=root $size=- fs=btrfs flags=
  done

i tried for ... in "${(@s.:.)partitions}", but that seems to join the
array before applying the (s.:.), which is contrary to my expectations,
but i may be misreading the manual (14.3.1: "[(@)] is distinct from
*field splitting* by the `f`, `s` or `z` flags, which still applies
within each array element.")

this is what i see:

  roman@stick ~ 1026:0 > echo $ZSH_VERSION
  5.0.2
  roman@stick ~ 1027:0 > typeset -p foos
  typeset -a foos
  foos=(a1:a2:a3a,a3b,a3c b1:b2:b3 :c2:c3)
  roman@stick ~ 1028:0 > for a b c in "${(@s.:.)foos}"; do typeset -p a b c; done
  typeset a=a1
  typeset b=a2
  typeset c='a3a,a3b,a3c b1'
  typeset a=b2
  typeset b='b3 '
  typeset c=c2
  typeset a=c3
  typeset b=''
  typeset c=''

and this is what i want:

  roman@stick ~ 1028:0 > for a b c in "${(???)foos}"; do typeset -p a b c; done
  typeset a=a1
  typeset b=a2
  typeset c=a3a,a3b,a3c
  typeset a=b1
  typeset b=b2
  typeset c=b3
  typeset a=''
  typeset b=c2
  typeset c=c3

-- 
roman


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

* Re: destructuring assignment
  2014-02-03 17:36 destructuring assignment Roman Neuhauser
@ 2014-02-03 18:00 ` Peter Stephenson
  2014-02-03 22:08   ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2014-02-03 18:00 UTC (permalink / raw)
  To: zsh-users

On Mon, 03 Feb 2014 18:36:00 +0100
Roman Neuhauser <neuhauser@sigpipe.cz> wrote:
> having an array such as
> 
>   declare -a partitions; partitions=(
>     /boot:boot:512:fat:boot,legacy_boot
>     :swap:10240:swap
>     /:root:-:btrfs
>   )
> 
> i'd love to iterate it like:
> 
>   for mountp name size fs flags in ...; do
>     # 1st iteration
>     # $mountp=/boot $name=boot $size=512 fs=fat flags=boot,legacy_boot
>     # 2nd iteration
>     # $mountp= $name=swap $size=10240 fs=swap flags=
>     # 3rd iteration
>     # $mountp=/ $name=root $size=- fs=btrfs flags=
>   done
> 
> i tried for ... in "${(@s.:.)partitions}", but that seems to join the
> array before applying the (s.:.)

It's all rather hairy and you've run up against ol' rule 10.

10. Forced joining
       If  the  `(j)'  flag is present, or no `(j)' flag is present but
       the string is to be split as given by rule 11., and joining  did
       not  take  place  at  step 5., any words in the value are joined
       together using the given string or the first character  of  $IFS
       if  none.  Note that the `(F)' flag implicitly supplies a string
       for joining in this manner.

Anyway, as you've written it above, you've still got the problem that
you've got variable numbers of colons in the line, and unless you can
think of some way of padding the invididual entries in the array (which
may be possible but won't be nice) a single loop won't handle that.
You'd need two loops.

for line in $partitions; do
  for mountp name size fs flags in "${(@s.:.)line}"; do
    print mountp=$mountp name=$name size=$size fs=$fs flags=$flags
  done
done

That does seem to work.

pws


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

* Re: destructuring assignment
  2014-02-03 18:00 ` Peter Stephenson
@ 2014-02-03 22:08   ` Bart Schaefer
  2014-02-04  2:53     ` setopt histignorespace not working Ray Andrews
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2014-02-03 22:08 UTC (permalink / raw)
  To: Zsh Users

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

On Mon, Feb 3, 2014 at 10:00 AM, Peter Stephenson
<p.stephenson@samsung.com>wrote:

> On Mon, 03 Feb 2014 18:36:00 +0100
> Roman Neuhauser <neuhauser@sigpipe.cz> wrote:
> > having an array such as
> >
> >   declare -a partitions; partitions=(
> >     /boot:boot:512:fat:boot,legacy_boot
> >     :swap:10240:swap
> >     /:root:-:btrfs
> >   )
>

If you were to put a trailing colon on the partitions with no mount flags,
you'd have the same number of colons in each array element, which might
make some other things easier (as PWS mentioned).

>   for mountp name size fs flags in ...; do
>

Given the trailing colons to create empty flags fields for the lines that
do not have flags, you can do

    for mountp name size fs flags in "${(@s.:.)${(j.:.)partitions}}"

However, I'd still prefer Peter's nested loops.


> for line in $partitions; do
>   for mountp name size fs flags in "${(@s.:.)line}"; do
>     print mountp=$mountp name=$name size=$size fs=$fs flags=$flags
>   done
> done
>

Another option is

while IFS=: read mountp name size fs flags; do
  # ...
done <<<${(F)partitions}

That should work even without the trailing colons, but could be tricky if
you need standard input inside the loop body.

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

* setopt histignorespace not working
  2014-02-03 22:08   ` Bart Schaefer
@ 2014-02-04  2:53     ` Ray Andrews
  2014-02-04  3:34       ` Ray Andrews
  0 siblings, 1 reply; 5+ messages in thread
From: Ray Andrews @ 2014-02-04  2:53 UTC (permalink / raw)
  To: zsh-users

Gentlemen,

Taking a closer look at my zshrc, I see "setopt histignorespace", but it 
doesn't work. I've spelled it with and without the underscores between 
words with no effect--regardless of  the leading space, the command is 
stored to history. What might be causing that?


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

* Re: setopt histignorespace not working
  2014-02-04  2:53     ` setopt histignorespace not working Ray Andrews
@ 2014-02-04  3:34       ` Ray Andrews
  0 siblings, 0 replies; 5+ messages in thread
From: Ray Andrews @ 2014-02-04  3:34 UTC (permalink / raw)
  To: zsh-users

On 03/02/14 06:53 PM, Ray Andrews wrote:
> Gentlemen,
>
> Taking a closer look at my zshrc, I see "setopt histignorespace", but 
> it doesn't work. I've spelled it with and without the underscores 
> between words with no effect--regardless of  the leading space, the 
> command is stored to history. What might be causing that?
>
Pardon me, I just found the answer. It's only there 'once' if I hit the 
up arrow, otherwise it is indeed missing from history. a dozen docs on 
the subject, but only one mentioned that fact ;-/


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

end of thread, other threads:[~2014-02-04  4:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-02-03 17:36 destructuring assignment Roman Neuhauser
2014-02-03 18:00 ` Peter Stephenson
2014-02-03 22:08   ` Bart Schaefer
2014-02-04  2:53     ` setopt histignorespace not working Ray Andrews
2014-02-04  3:34       ` Ray Andrews

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