comp.lang.ada
 help / color / mirror / Atom feed
From: david.c.hoos.sr@ada95.com
Subject: Re: Parsing a line into strings
Date: 1998/07/11
Date: 1998-07-11T00:00:00+00:00	[thread overview]
Message-ID: <6o6e34$bl$1@nnrp1.dejanews.com> (raw)
In-Reply-To: 35A3A199.D55C3153@oit.edu

In article <35A3A199.D55C3153@oit.edu>,
  C N <netzelc@oit.edu> wrote:
> Hi all,
>
>     I've been looking for a comand in Ada that is equivilent to C's
> "strtok" .  So far - no luck.
> I need to break up a line that is read in from a file with the strings
> delimited by comas and spaces.
>
>     If this command does'nt exhist, would anyone be willing to share a
> chunk of code they've developed that performs this operation?
>
>     Any and all responses are greatly appreaciated,
>
> -=Chris=-
>
> -=// "Even a blind squrrel finds a nut once in a while." \\=-
>

The program at the end of this message will parse the string submitted
as the first command line argument into tokens delimited by any of the
characters in the string submitted as the second command line
argument, and print the tokens one at a time to the standard output.

--- begin program source code ----
with Ada.Command_Line;
with Ada.Strings.Fixed;
with Ada.Strings.Maps;
with Ada.Text_Io;
procedure Tokenize is
begin
   if Ada.Command_Line.Argument_Count /= 2 then
      Ada.Text_Io.Put_Line (Ada.Text_Io.Standard_Error,
         "USAGE:" &
         Ada.Command_Line.Command_Name &
         " <string to be tokenized> <delimiters>");
      Ada.Command_Line.Set_Exit_Status (Ada.Command_Line.Failure);
      return;
   end if;
   declare
      The_String : constant String := Ada.Command_Line.Argument (1);
      The_Delimiters : constant Ada.Strings.Maps.Character_Set :=
      Ada.Strings.Maps.To_Set (Ada.Command_Line.Argument(2));
      First : Positive;     -- Index of first character in token
      Last : Natural := 0;  -- Index of last character in token (also used
      -- incremented by 1 -- as the starting point for next search).
   begin
      loop
         Ada.Strings.Fixed.Find_Token
         (Source => The_String (Last + 1 .. The_String'Last),
            Set => The_Delimiters,
            Test => Ada.Strings.Outside,
            First => First,
            Last => Last);
         exit when Last = 0;
         Ada.Text_Io.Put_Line
         ("Token => """ & The_String (First .. Last) & """");
      end loop;
   end;
end Tokenize;
--- end program source code ----


-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum




      parent reply	other threads:[~1998-07-11  0:00 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-07-08  0:00 Parsing a line into strings C N
1998-07-08  0:00 ` Samuel Tardieu
1998-07-08  0:00   ` C N
1998-07-09  0:00     ` Dmitriy Anisimkov
1998-07-08  0:00       ` Brian Rogoff
1998-07-08  0:00         ` Robert Dewar
1998-07-09  0:00           ` Brian Rogoff
1998-07-08  0:00         ` nabbasi
1998-07-09  0:00           ` Robert Dewar
1998-07-09  0:00             ` Michael F Brenner
1998-07-14  0:00             ` Norman H. Cohen
1998-07-15  0:00               ` Robert I. Eachus
1998-07-18  0:00                 ` Robert Dewar
1998-07-09  0:00 ` Dale Stanbrough
1998-07-09  0:00 ` dennison
1998-07-09  0:00 ` Darren Davenport
1998-07-10  0:00   ` Robert I. Eachus
1998-07-11  0:00 ` david.c.hoos.sr [this message]
replies disabled

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