zsh-users
 help / color / mirror / code / Atom feed
* get array of integers?
@ 2006-09-03 16:59 sam reckoner
  2006-09-03 17:55 ` Bart Schaefer
  2006-09-03 20:27 ` Roman Cheplyaka
  0 siblings, 2 replies; 4+ messages in thread
From: sam reckoner @ 2006-09-03 16:59 UTC (permalink / raw)
  To: zsh-users

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

I have been doing

x=$(){1..10}

to get a range of integers. Is there a better way? Also, how can I get a
range that steps by 2 instead of by 1 as in

1 3 5 7 ...

Thanks!

[-- Attachment #2: Type: text/html, Size: 209 bytes --]

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

* Re: get array of integers?
  2006-09-03 16:59 get array of integers? sam reckoner
@ 2006-09-03 17:55 ` Bart Schaefer
  2006-09-03 20:27 ` Roman Cheplyaka
  1 sibling, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2006-09-03 17:55 UTC (permalink / raw)
  To: zsh-users

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.


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

* Re: get array of integers?
  2006-09-03 16:59 get array of integers? sam reckoner
  2006-09-03 17:55 ` Bart Schaefer
@ 2006-09-03 20:27 ` Roman Cheplyaka
  2006-09-03 21:34   ` Dan Nelson
  1 sibling, 1 reply; 4+ messages in thread
From: Roman Cheplyaka @ 2006-09-03 20:27 UTC (permalink / raw)
  To: zsh-users

On Sun, Sep 03, 2006 at 09:59:21AM -0700, sam reckoner wrote:
>    I have been doing
> 
>    x=$(){1..10}
> 
>    to get a range of integers. Is there a better way? Also, how can I get a
>    range that steps by 2 instead of by 1 as in
> 
>    1 3 5 7 ...
Take a look at seq(1).

-- 
Roman I. Cheplyaka


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

* Re: get array of integers?
  2006-09-03 20:27 ` Roman Cheplyaka
@ 2006-09-03 21:34   ` Dan Nelson
  0 siblings, 0 replies; 4+ messages in thread
From: Dan Nelson @ 2006-09-03 21:34 UTC (permalink / raw)
  To: Roman Cheplyaka; +Cc: zsh-users

In the last episode (Sep 03), Roman Cheplyaka said:
> On Sun, Sep 03, 2006 at 09:59:21AM -0700, sam reckoner wrote:
> >    I have been doing
> > 
> >    x=$(){1..10}
> > 
> >    to get a range of integers. Is there a better way? Also, how can
> >    I get a range that steps by 2 instead of by 1 as in
> > 
> >    1 3 5 7 ...
>
> Take a look at seq(1).

or jot(1) , depending on what OS you are on.

-- 
	Dan Nelson
	dnelson@allantgroup.com


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

end of thread, other threads:[~2006-09-03 21:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-09-03 16:59 get array of integers? sam reckoner
2006-09-03 17:55 ` Bart Schaefer
2006-09-03 20:27 ` Roman Cheplyaka
2006-09-03 21:34   ` Dan Nelson

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