zsh-users
 help / color / mirror / code / Atom feed
From: Peter Stephenson <p.stephenson@samsung.com>
To: zsh-users@zsh.org
Subject: Re: destructuring assignment
Date: Mon, 03 Feb 2014 18:00:49 +0000	[thread overview]
Message-ID: <20140203180049.775877d9@pwslap01u.europe.root.pri> (raw)
In-Reply-To: <20140203173600.GG14992@isis.sigpipe.cz>

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


  reply	other threads:[~2014-02-03 18:11 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-02-03 17:36 Roman Neuhauser
2014-02-03 18:00 ` Peter Stephenson [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20140203180049.775877d9@pwslap01u.europe.root.pri \
    --to=p.stephenson@samsung.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).