From: Jeffrey Carter <jrcarter@acm.org>
Subject: Re: "In reverse" in for loop acting weird.
Date: Sun, 29 Sep 2002 18:49:07 GMT
Date: 2002-09-29T18:49:07+00:00 [thread overview]
Message-ID: <3D974B39.6000800@acm.org> (raw)
In-Reply-To: PFGl9.81453$5M3.3538442@e3500-atl2.usenetserver.com
gilrain wrote:
> Hi all,
>
> Okay, my program is working perfectly except for this one minor detail. The
> result (stored in a string) prints exactly backward. What's baffling me is
> that it's still backward even when I reverse the process for building the
> string. Here's the offending code segment:
>
> for j in reverse -15..15 loop
> case integer(abs(coefficients(j))) is
> when 0 => result(j+16) := '0';
> when 1 => result(j+16) := '1';
> when 2 => result(j+16) := '2';
> when 3 => result(j+16) := '3';
> when 4 => result(j+16) := '4';
> when 5 => result(j+16) := '5';
> when 6 => result(j+16) := '6';
> when 7 => result(j+16) := '7';
> when 8 => result(j+16) := '8';
> when 9 => result(j+16) := '9';
> when 10 => result(j+16) := 'A';
> when 11 => result(j+16) := 'B';
> when 12 => result(j+16) := 'C';
> when 13 => result(j+16) := 'D';
> when 14 => result(j+16) := 'E';
> when 15 => result(j+16) := 'F';
> when 16 => result(j+16) := 'G';
> when others => result(j+16) := '!';
> end case;
> end loop; result(16) := '.';
>
> Coefficients is an array of long floats which at this point in the program
> only contains whole numbers. I want coefficients(15) to be the first
> character in the string, and so on to -15. The above code prints it
> backwards, but the thing which gets me is that taking out "reverse" doesn't
> change the program output one bit!
This little excerpt doesn't "print" anything, so understandably we're a
little confused about what you're talking about. Something that shows
the object and type declarations referenced, the output statements
involved, the output obtained, and the output expected will likely get a
more useful response.
Of the snippet provided above, it appears that you will put the same
values in result regardless of the order that you traverse Coefficients,
whether normally, reverse, or some sort of non-repeating random selection.
If the range of Coefficients is -15 .. 15, then your for loop would be
better written as
for J in [reverse] Coefficients'range loop
In general, when working with arrays, it is rarely necessary to mention
a magic number more than once. Once the magic number is used to define
the range of the type or object, attributes, such as 'range above,
eliminate the need to mention it again.
When you do need to mention a magic number more than once, it's a good
idea to make it a named number or constant. For example, the 16 in your
code is a good candidate for this. I suspect it may be
(Result'Length + 1) / 2.
--
Jeff Carter
"Nobody expects the Spanish Inquisition!"
Monty Python's Flying Circus
next prev parent reply other threads:[~2002-09-29 18:49 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-09-29 17:28 "In reverse" in for loop acting weird gilrain
2002-09-29 17:59 ` Martin Dowie
2002-09-29 21:15 ` Martin Dowie
2002-09-29 18:31 ` tmoran
2002-09-29 19:17 ` gilrain
2002-09-29 18:49 ` Jeffrey Carter [this message]
2002-09-29 19:19 ` gilrain
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox