comp.lang.ada
 help / color / mirror / Atom feed
From: Kenneth Wolcott <kennethwolcott@gmail.com>
Subject: Re: When using the Ada Big_Numbers.Big_Integers package, can the To_String function output be sliced?
Date: Thu, 27 Jul 2023 15:47:01 -0700 (PDT)	[thread overview]
Message-ID: <be16b58d-d612-4d48-aae1-062818184a66n@googlegroups.com> (raw)
In-Reply-To: <u9tbaf$1r81p$1@dont-email.me>

On Thursday, July 27, 2023 at 1:53:38 AM UTC-7, Jeffrey R.Carter wrote:
> On 2023-07-27 07:26, Kenneth Wolcott wrote: 
> > 
> > When using the Ada Big_Numbers.Big_Integers package, can the To_String function output be sliced? 
> > 
> > I'd like to slice out the first two digits from the output of the To_String function of Ada.Big_Numbers.Big_Integers. 
> > 
> > When trying to do the usual string slicing on the output I get a silent failure, it is just as if I typed the null statement.
> It would be helpful if you could show what you're doing and what results you 
> get. This 
> 
> with Ada.Numerics.Discrete_Random; 
> with Ada.Text_IO; 
> 
> procedure Func_Slice is 
> subtype Digit is Character range '0' .. '9'; 
> 
> package Random is new Ada.Numerics.Discrete_Random (Result_Subtype => Digit); 
> 
> Gen : Random.Generator; 
> 
> function Image (N : in Natural) return String is 
> (if N = 0 then "" else Random.Random (Gen) & Image (N - 1) ); 
> -- Returns a string of N random Digits 
> begin -- Func_Slice 
> Random.Reset (Gen => Gen); 
> Ada.Text_IO.Put_Line (Item => Image (10) (1 .. 2) ); 
> end Func_Slice; 
> 
> works as expected 
> 
> $ gnatmake -m -j0 -gnat12 -gnatan -gnato2 -O2 -fstack-check func_slice.adb 
> x86_64-linux-gnu-gcc-12 -c -gnat12 -gnatan -gnato2 -O2 -fstack-check func_slice.adb 
> x86_64-linux-gnu-gnatbind-12 -x func_slice.ali 
> x86_64-linux-gnu-gnatlink-12 func_slice.ali -O2 -fstack-check 
> $ ./func_slice 
> 10 
> $ ./func_slice 
> 51 

Hi Jeff;

  After going back and exhaustively reviewing the AdaCore Learning Introduction sections on Strings and Arrays, I determined that I was not slicing with the correct syntax for what I wanted to obtain, it seems that I was slicing built-in whitespace, not the digits I was looking for.  So I am now extracting the two-digit slice using (2 .. 3) rather than (1 .. 2) and I am now obtaining the desired output.  What I was trying to do was to obtain the slice from the string on the fly, not first saving the string to a variable.

This is what I have now (which works great):
---------------------------------------------------------------------------
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Numerics.Big_Numbers.Big_Integers; use Ada.Numerics.Big_Numbers.Big_Integers;

procedure Powers_Of_2_With_Leading_Digits_Of_12 is
  Max : Positive := Integer'Value (Argument (1));
  Powers_of_2 : Big_Positive := 1;
begin
  for I in 1 .. Max loop
    Powers_of_2 := Powers_of_2 * To_Big_Integer (2);
    if I >= 4 then
      declare
        Powers_of_2_in_String_Format : String := To_String (Powers_of_2);
        First_Pair_of_String_Digits : String := Powers_of_2_in_String_Format (2 .. 3);
      begin
        if First_Pair_of_String_Digits = "12" then
          Put ("2^");
          Put (I, 0);
          Put_Line (To_String (Powers_of_2));
        end if;
      end; -- declare
    end if;
  end loop;
end Powers_Of_2_With_Leading_Digits_Of_12;
---------------------------------------------------------------------------

Now I can generalize this to more closely match the task requirements.

I think I tried to incorrectly create the slice from the To_String output directly without creating the variable in which to store the slice, which then requires the declare block.

Thanks,
Ken

  reply	other threads:[~2023-07-27 22:47 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-27  5:26 When using the Ada Big_Numbers.Big_Integers package, can the To_String function output be sliced? Kenneth Wolcott
2023-07-27  8:53 ` Jeffrey R.Carter
2023-07-27 22:47   ` Kenneth Wolcott [this message]
2023-07-28 17:19     ` AdaMagica
2023-07-28 18:21     ` Simon Wright
2023-07-29  3:29       ` Kenneth Wolcott
2023-07-29 11:07       ` AdaMagica
2023-07-29 23:49         ` Kenneth Wolcott
2023-07-29 23:53           ` Kenneth Wolcott
2023-07-30  4:43             ` Randy Brukardt
2023-07-31 19:36               ` Kenneth Wolcott
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox