zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@sunsite.dk
Subject: Re: get array of integers?
Date: Sun, 03 Sep 2006 10:55:07 -0700	[thread overview]
Message-ID: <060903105507.ZM7636@torch.brasslantern.com> (raw)
In-Reply-To: <6a42eec70609030959t2a764bfm36fea2c4783f4f01@mail.gmail.com>

On Sep 3,  9:59am, sam reckoner wrote:
} 
} I have been doing
} 
} x=$(){1..10}

That's a typo?  You mean

x=( {1..10} )

} Is there a better way?

Probably not, except maybe to skip generating the array at all and use a
"for (( x=1; x <= 10; ++x ))" loop.  Depends on context, of course.
(Make sure "x" is not already defined as an array, or that loop will
not work properly.  Best to explicitly declare "integer x".)

} Also, how can I get a range that steps by 2 instead of by 1 as in
} 
} 1 3 5 7 ...

By being sneaky.

typeset -A x
x=( {1..10} )

Now ${(k)x} is an array of odd numbers, and ${(v)x} is an array of
even numbers.  To get them in ascending order, use ${(nok)x}.  Note
that you have to assign an even number of numbers, that is

x=( {1..9} )

will print an error message and not do the assignment.

Of course this trick doesn't work if you want an array by anything
other than twos.  So a more general approach (if you really want to
avoid that "for" loop) would be

typeset -a x

# Count by 7s from 7 to 70
x=( '$(('{1..10}'*7))' )
x=( ${(e)x} )

# Count by 5s from 1 to 51
x=( '$(('{1..10}'*5+1))' )
x=( 1 ${(e)x} )

Of course you can also get really silly:

# Count by 3s from 3 to 30
setopt extendedglob
typeset -a x
x=( {1..10} )
x=( ${x//(#b)(*)/$(($match*3))} )

That's doing a lot of extra work behind the scenes, though, to match
the pattern, store the backreference, etc.


  reply	other threads:[~2006-09-03 17:55 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-09-03 16:59 sam reckoner
2006-09-03 17:55 ` Bart Schaefer [this message]
2006-09-03 20:27 ` Roman Cheplyaka
2006-09-03 21:34   ` Dan Nelson

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=060903105507.ZM7636@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@sunsite.dk \
    /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).