The Perl range operator '..' is often used to quickly fill in arrays like so: @arr = ( 1..9 );. Unfortunately that only works when the left argument is smaller than the right argument and you can only make an array with ascending values. But what if you want descending values?
@array = ( 7..1 ); # returns an empty array @array = map { $_ * -1 } ( -7..-1 ); # When generating the array you are technically going # up from a large negative to a small negative. # Then, you multiply each value by -1 and assign # to @array. Works like a charm.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Autogenerate descending arrays
by jweed (Chaplain) on Mar 13, 2004 at 21:08 UTC | |
Re: Autogenerate descending arrays
by pbeckingham (Parson) on Mar 13, 2004 at 21:12 UTC | |
Re: Autogenerate descending arrays
by TomDLux (Vicar) on Mar 13, 2004 at 21:25 UTC | |
Re: Autogenerate descending arrays
by kappa (Chaplain) on Mar 14, 2004 at 21:57 UTC | |
Re: Autogenerate descending arrays
by bl0rf (Pilgrim) on Mar 14, 2004 at 21:52 UTC | |
by kappa (Chaplain) on Mar 14, 2004 at 22:08 UTC | |
by ysth (Canon) on Mar 14, 2004 at 23:25 UTC | |
by kappa (Chaplain) on Mar 15, 2004 at 14:30 UTC |
Back to
Cool Uses for Perl