From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,782d6c8a12248b09 X-Google-Attributes: gid103376,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!e17g2000hsg.googlegroups.com!not-for-mail From: Adam Beneschan Newsgroups: comp.lang.ada Subject: Re: Ada Recursion with strings Date: Tue, 30 Sep 2008 15:24:40 -0700 (PDT) Organization: http://groups.google.com Message-ID: References: NNTP-Posting-Host: 66.126.103.122 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1222813480 7994 127.0.0.1 (30 Sep 2008 22:24:40 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 30 Sep 2008 22:24:40 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: e17g2000hsg.googlegroups.com; posting-host=66.126.103.122; posting-account=duW0ogkAAABjRdnxgLGXDfna0Gc6XqmQ User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.7.12) Gecko/20050922 Fedora/1.7.12-1.3.1,gzip(gfe),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:2174 Date: 2008-09-30T15:24:40-07:00 List-Id: On Sep 30, 1:30 pm, Joe wrote: > Hey all, > I'm trying to write a function that converts a 2**32 mod type into an > string formatted like an IP address (i.e. 192.168.2.1). > > Here's what I wrote > > with Ada.Text_IO; > use Ada.Text_IO; > > procedure Ip_packet is > type Word_Type is mod 2**32; > > function IP_String (Word : Word_Type; > IP_String : String := "") return String > is > Sub_String : String := Word_Type'Image(Word mod 2**8); > begin > if Word <= 2**8 then > return Word_Type'Image(Word) & IP_String; > else > return IP_String( Word / 2**8, > "." & Sub_String & IP_String); > end if; > end IP_String; > > begin -- Ip_packet > Put(IP_String(123456)); > end Ip_packet; > > There are 2 errors with this that I don't understand. First, Ada > expects an integer for (word / 2**8). Is this because word_type is > outside the standard integer range? How do I get a division operator > that works on word_type? > > If I patch the integer error by type casting word to an integer I get > another error. On the second return statement, Ada complains that > there are "too many subscripts in array reference". Is there a > limitation on the string I can pass to a recursive function call? When you say this: return IP_String( Word / 2**8, "." & Sub_String & IP_String); what is the compiler going to think you mean by the first IP_String in this expression? What about the second one? There are other errors in your code; you'll probably figure several of them out once you get your program to run, and you'll probably figure another out if you change 123456 in your test to 65536. -- Adam