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=-3.2 required=3.0 tests=BAYES_00,NICE_REPLY_A, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 Path: eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: =?UTF-8?Q?Bj=c3=b6rn_Lundin?= Newsgroups: comp.lang.ada Subject: Re: Non-standard functions in GNAT's Ada.Containers packages? Date: Fri, 16 Sep 2022 20:53:07 +0200 Organization: A noiseless patient Spider Message-ID: References: <6a8a1468-5148-49d7-b342-327f8eaf8097n@googlegroups.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Fri, 16 Sep 2022 18:53:07 -0000 (UTC) Injection-Info: reader01.eternal-september.org; posting-host="6b4ce771fba3c91bea5cb1473b29fd63"; logging-data="4121409"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19bOAxJlEHbDNcvBNjeNfgx" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Cancel-Lock: sha1:vHnFerV7cwQ1AIrBElZMk9q6q4w= In-Reply-To: Content-Language: en-US Xref: reader01.eternal-september.org comp.lang.ada:64353 List-Id: On 2022-09-16 17:00, Marius Amado-Alves wrote: >>>> "for X of M loop ... end loop". >>> >>> Not possible for maps. >> but you can as >>> https://programming-idioms.org/idiom/13/iterate-over-map-keys-and-value/1511/ada> >> >> with Ada.Containers.Indefinite_Hashed_Maps; >> with Ada.Strings.Hash; >> use Ada.Containers; >> for C in My_Map.Iterate loop >> Put_Line ("Key = " & Key (C) & ", Value = " & Element (C)); >> end loop; > > Thanks, but this is "in", not "of", requires cursors, and DOES NOT COMPILE, as probably needs like ten lines of boiler plate not show. well, yes . I thought the for looping stuff was the important part, since you done want to call Next. in or of - does it really matter here? for C in My_Map.Iterate loop or for C of My_Map loop is not that hurtful to my eyes. for K,V in My_Map.Iterate loop or for K,V of My_Map loop getting Key and Value as a tuple would be nicer of course Anyway, I forgot to instantiate the map. Here's a compileable variant It outputs Key = AA, Value = 1 Key = AB, Value = 5 ---------------- with Ada.Containers.Hashed_Maps; with Ada.Strings.Hash; use Ada.Containers; with Text_Io ; use Text_IO; procedure Ite is subtype Test_Type is String(1..2); package Test_Map_Pkg is new Ada.Containers.Hashed_Maps (Test_Type, Integer, Ada.Strings.Hash, "=", "="); My_Map : Test_Map_Pkg.Map; use Test_Map_Pkg; begin My_Map.Insert("AA",1); My_Map.Insert("AB",5); for C in My_Map.Iterate loop Put_Line ("Key = " & Key (C) & ", Value = " & Element (C)'Img); end loop; end Ite; -- /Björn