1

Can anyone please tell me the usage of Insert in Ada language. I had tried the net but i couldn't just figure out. It would be a great help if anyone could provide me with examples too.

Thanks in advance Maddy

progrmr
  • 75,956
  • 16
  • 112
  • 147
maddy
  • 827
  • 2
  • 11
  • 19
  • Maddy, I think we need a bit more context! Do you mean one of the Inserts in Ada.Strings? or in Ada.Containers? Also, SO has formatted most of your question as code because you've preceded the text with a number of spaces (>= 4). Much better not to put leading spaces in. – Simon Wright Jun 15 '10 at 10:58
  • @Simon Wright...I need inserts in ada.Strings.It would be better if you could provide me a more generalised overview of the insert used. Regarding the formatting part,i shall take care of it the next time. – maddy Jun 15 '10 at 11:12
  • I don't mean to sound like a jerk here, but your question is so basic, and so simple, that one barely knows where to begin. If you can't figure it out yourself by simply reading the very clear description in the reference manual, and maybe trying out some calls of it in a test program, then your understanding of Ada is so lacking that one is hard-pressed to know how to explain it in any sufficiently simple way. Sorry. – Marc C Jun 15 '10 at 18:00
  • "Insert" isn't a keyword, and IIR appears in multiple libraries (including standard ones) as a subroutine name, so you really need to clarify this. – T.E.D. Jun 25 '10 at 05:24

1 Answers1

3

I've never used Insert before (don't often have to do anything but a simple Put_Line with strings) so knocked this together:

with Ada.Strings.Fixed;
with Ada.Text_IO; use Ada.Text_IO;
procedure Inserting is
   Base : constant String := (1 .. 8 => ' ') & "aaaaaaaa" & (1 .. 8 => ' ');
begin
   Put_Line (ASCII.HT & '|' & Base & '|');
   for J in 1 .. 24 loop
      declare
         S : String := Base;
      begin
         Ada.Strings.Fixed.Insert (S,
                                   Before => J,
                                   New_Item => "ccc");
         Put_Line (Integer'Image (J) & ASCII.HT & '|' & S & '|');
      end;
   end loop;
end Inserting;

and the output is

       |        aaaaaaaa        |
 1     |ccc        aaaaaaaa     |
 2     | ccc       aaaaaaaa     |
 3     |  ccc      aaaaaaaa     |
 4     |   ccc     aaaaaaaa     |
 5     |    ccc    aaaaaaaa     |
 6     |     ccc   aaaaaaaa     |
 7     |      ccc  aaaaaaaa     |
 8     |       ccc aaaaaaaa     |
 9     |        cccaaaaaaaa     |
 10    |        acccaaaaaaa     |
 11    |        aacccaaaaaa     |
 12    |        aaacccaaaaa     |
 13    |        aaaacccaaaa     |
 14    |        aaaaacccaaa     |
 15    |        aaaaaacccaa     |
 16    |        aaaaaaaccca     |
 17    |        aaaaaaaaccc     |
 18    |        aaaaaaaa ccc    |
 19    |        aaaaaaaa  ccc   |
 20    |        aaaaaaaa   ccc  |
 21    |        aaaaaaaa    ccc |
 22    |        aaaaaaaa     ccc|

raised ADA.STRINGS.LENGTH_ERROR : a-strfix.adb:358

which I hope gives the general flavour.

The Ada95AARM A.4.3 at http://www.adaic.com/standards/95aarm/html/AA-A-4-3.html (3) tells about the concepts behind this.

Simon Wright
  • 25,108
  • 2
  • 35
  • 62