From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on ip-172-31-65-14.ec2.internal X-Spam-Level: X-Spam-Status: No, score=-1.9 required=3.0 tests=BAYES_00,FREEMAIL_FROM, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Received: by 2002:a05:622a:1a13:b0:400:9f40:e4f4 with SMTP id f19-20020a05622a1a1300b004009f40e4f4mr3679qtb.6.1690498022522; Thu, 27 Jul 2023 15:47:02 -0700 (PDT) X-Received: by 2002:a05:6870:1aaf:b0:1bb:5605:df92 with SMTP id ef47-20020a0568701aaf00b001bb5605df92mr1012601oab.10.1690498022140; Thu, 27 Jul 2023 15:47:02 -0700 (PDT) Path: eternal-september.org!news.eternal-september.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Thu, 27 Jul 2023 15:47:01 -0700 (PDT) In-Reply-To: Injection-Info: google-groups.googlegroups.com; posting-host=98.59.238.112; posting-account=oHOvdQoAAACYgyEBjgPNvKFOGxg8pNns NNTP-Posting-Host: 98.59.238.112 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: When using the Ada Big_Numbers.Big_Integers package, can the To_String function output be sliced? From: Kenneth Wolcott Injection-Date: Thu, 27 Jul 2023 22:47:02 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Received-Bytes: 4867 Xref: news.eternal-september.org comp.lang.ada:65460 List-Id: On Thursday, July 27, 2023 at 1:53:38=E2=80=AFAM UTC-7, Jeffrey R.Carter wr= ote: > On 2023-07-27 07:26, Kenneth Wolcott wrote:=20 > >=20 > > When using the Ada Big_Numbers.Big_Integers package, can the To_String = function output be sliced?=20 > >=20 > > I'd like to slice out the first two digits from the output of the To_St= ring function of Ada.Big_Numbers.Big_Integers.=20 > >=20 > > 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=20 > get. This=20 >=20 > with Ada.Numerics.Discrete_Random;=20 > with Ada.Text_IO;=20 >=20 > procedure Func_Slice is=20 > subtype Digit is Character range '0' .. '9';=20 >=20 > package Random is new Ada.Numerics.Discrete_Random (Result_Subtype =3D> D= igit);=20 >=20 > Gen : Random.Generator;=20 >=20 > function Image (N : in Natural) return String is=20 > (if N =3D 0 then "" else Random.Random (Gen) & Image (N - 1) );=20 > -- Returns a string of N random Digits=20 > begin -- Func_Slice=20 > Random.Reset (Gen =3D> Gen);=20 > Ada.Text_IO.Put_Line (Item =3D> Image (10) (1 .. 2) );=20 > end Func_Slice;=20 >=20 > works as expected=20 >=20 > $ gnatmake -m -j0 -gnat12 -gnatan -gnato2 -O2 -fstack-check func_slice.ad= b=20 > x86_64-linux-gnu-gcc-12 -c -gnat12 -gnatan -gnato2 -O2 -fstack-check func= _slice.adb=20 > x86_64-linux-gnu-gnatbind-12 -x func_slice.ali=20 > x86_64-linux-gnu-gnatlink-12 func_slice.ali -O2 -fstack-check=20 > $ ./func_slice=20 > 10=20 > $ ./func_slice=20 > 51=20 Hi Jeff; After going back and exhaustively reviewing the AdaCore Learning Introduc= tion sections on Strings and Arrays, I determined that I was not slicing wi= th the correct syntax for what I wanted to obtain, it seems that I was slic= ing built-in whitespace, not the digits I was looking for. So I am now ext= racting the two-digit slice using (2 .. 3) rather than (1 .. 2) and I am no= w 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.Bi= g_Integers; procedure Powers_Of_2_With_Leading_Digits_Of_12 is Max : Positive :=3D Integer'Value (Argument (1)); Powers_of_2 : Big_Positive :=3D 1; begin for I in 1 .. Max loop Powers_of_2 :=3D Powers_of_2 * To_Big_Integer (2); if I >=3D 4 then declare Powers_of_2_in_String_Format : String :=3D To_String (Powers_of_2); First_Pair_of_String_Digits : String :=3D Powers_of_2_in_String_For= mat (2 .. 3); begin if First_Pair_of_String_Digits =3D "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 d= irectly without creating the variable in which to store the slice, which th= en requires the declare block. Thanks, Ken