Board index » delphi » Extract numbers from strings

Extract numbers from strings

Hi,

I have a series of strings (loaded from a text file), which look
something like

25 33  722
  2     35  9
etc.

I need to extract the numbers,
first 25, 33, 722
next 2, 35, 9
etc.

I would like to achieve similar effect to the old readln(file, v1, v2,
v3).  Is there a simple way of extracting the numbers, without having
to look for first non-blank, then next blank, then convert to number
etc.?  

There should be a simple routine in Delphi to do this.  Is there?  

What is needed is some reciprocal form of "Format" function.  Is there
something like that?

TIA,

Al.

A. Kabaila    http://www.pcug.org.au/~akabaila
akaba...@pcug.org.au

 

Re:Extract numbers from strings


In article <il0hess6cfe3l0223pc1jjg4l5grub1...@4ax.com>, Charles Lynx
(ch_l...@yahoo.com) says...

Quote
> I would like to achieve similar effect to the old readln(file, v1, v2,
> v3).  Is there a simple way of extracting the numbers, without having
> to look for first non-blank, then next blank, then convert to number
> etc.?  

Sorry for asking a dumb question, but if readln() works, why not use it?

M.

--
ROOTS 2000, Bergen, April 27-28
{*word*19}burn, Coplien, Fowler, Groven, Reenskaug, Reich, Nygaard
Patterns, Use Cases, Refactoring, XP, UML, OO ++
http://roots.dnd.no

Re:Extract numbers from strings


Quote
>I have a series of strings (loaded from a text file), which look
>something like

>25 33  722
>  2     35  9
>etc.

>I need to extract the numbers,
>first 25, 33, 722
>next 2, 35, 9
>etc.

var
list:TstringList;
begin
    List:=TstringList.create;
    List.commatext:='25 33  722';
    showmessage(List.text);
    List.free;
end;

/Morten

Re:Extract numbers from strings


Quote
"Morten Lassen" <morten.las...@danbbs.dk> wrote:

>>25 33  722
>>I need to extract the numbers,

Evidently I did not explain myself - I need to extract 25 (as a
number), 33 and 722 as separate numbers (first strings, then converted
to double type).

Quote
>var
>list:TstringList;
>begin
>    List:=TstringList.create;
>    List.commatext:='25 33  722';
>    showmessage(List.text);
>    List.free;
>end;

>/Morten

Thanks for your attempt to help, Morten.

Al.

A. Kabaila    http://www.pcug.org.au/~akabaila
akaba...@pcug.org.au

Re:Extract numbers from strings


In article <il0hess6cfe3l0223pc1jjg4l5grub1...@4ax.com>, Charles Lynx

Quote
<ch_l...@yahoo.com> writes:
>I have a series of strings (loaded from a text file), which look
>something like

>25 33  722
>  2     35  9
>etc.

>I need to extract the numbers,
>first 25, 33, 722
>next 2, 35, 9
>etc.

>I would like to achieve similar effect to the old readln(file, v1, v2,
>v3).  Is there a simple way of extracting the numbers, without having
>to look for first non-blank, then next blank, then convert to number
>etc.?  

>There should be a simple routine in Delphi to do this.  Is there?  

>What is needed is some reciprocal form of "Format" function.  Is there
>something like that?

No but you can use the CommaText property of a TStringList to achieve what you
want. CommaText splits (or makes) a string at the spaces into separate Items of
a TStringList

var
  SL1, SL2 : TStringList;
  i, j, Var1, Var2, Var3 : integer;
begin
  SL1 := TStringList.Create;
  SL2 := TStringList.Create;
  SL1.LoadfromFile('DataFile.txt');  // each stringlist item is one line from
the file
  for i := 0 to SL1.Count - 1 do begin
    SL2.Clear;
    SL2.CommaText := SL1.Items[i]; // line is split at space - now its one
number per item
    Var1 := StrToInt(SL2.Items[0]);
    Var2 := StrToInt(SL2.Items[1]);
    Var3 := StrToInt(SL2.Items[2]);
    //
    // ... now you have to do something with these variables before the next
loop
    //
  end; {for i := 0 to SL1.Count - 1}
  SL1.Free;
  SL2.Free
end;

Alan Lloyd
alangll...@aol.com

Re:Extract numbers from strings


Quote
>Evidently I did not explain myself - I need to extract 25 (as a
>number), 33 and 722 as separate numbers (first strings, then converted
>to double type).

how about this, then?

var
list:TstringList;
t:integer;
numbers:array of integer;
begin
    List:=TstringList.create;
    List.commatext:='25 33  722';
    SetLength(numbers,list.count);
    for t:=0 to list.count-1 do
    begin
            Try
                numbers[t]:=strtoint(list[t]);
            except on eConvertError do numbers[t]:=0;
            end;
    end;
    List.free;
end;

/Morten

Re:Extract numbers from strings


JRS:  In article <il0hess6cfe3l0223pc1jjg4l5grub1...@4ax.com> of Mon, 3
Apr 2000 21:41:26 seen in news:alt.comp.lang.borland-delphi, Charles

Quote
Lynx <ch_l...@yahoo.com> wrote:
>I have a series of strings (loaded from a text file), which look
>something like

>25 33  722
>  2     35  9
>etc.

>I need to extract the numbers,
>first 25, 33, 722
>next 2, 35, 9
>etc.

>I would like to achieve similar effect to the old readln(file, v1, v2,
>v3).  Is there a simple way of extracting the numbers, without having
>to look for first non-blank, then next blank, then convert to number
>etc.?  

I believe that it would be both useful and easy to arrange that file
operations could also be string operations, for all types of string.

I still think in DOS/TP/BP terms - so what I mean is that the compiler
should be able to include in a program something resembling, in effect
but maybe not in mechanism, a driver.  We have the procedure
        Assign(var F; String) ;
let us also have
        AssignStr(var F; String) ;
where the string, rather than holding the name of the data, holds the
data itself.

Since Read(F, ...) and Write(F, ...) don't actually read/write to the
disc, but to a buffer, ISTM that it should be practical to read/write to
the string itself.  Functions EoF, EoLn should map over; Rename and
Erase would not.  Errors should map.

Procedure Str would hardly be needed; procedure Val could be used less
often; but both would be maintained for compatibility.

Here's an example : to write the date in BP7 to a file I can use
        Write(F, YYYY, '/', MM, '/', DD) ;
but to write it to a graphic screen I need (ISTR) three calls to Str
before constructing a string from the temporaries.  Similarly, IIRC, for
writing to a Canvas in Delphi, though for output there is Format.

So (unless included in a much-wanted but highly-unexpected BP7.5) the
advantages of this would be in input rather than output, and in visual
simplicity & generality.

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
 <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
 <URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ;
 <URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ.

Re:Extract numbers from strings


In article <sTHHojBaEK74E...@merlyn.demon.co.uk>, Dr John Stockton

Quote
<j...@merlyn.demon.co.uk> writes:
>        AssignStr(var F; String) ;
>where the string, rather than holding the name of the data, holds the
>data itself.

Isn't that effectively what a TStringStream encapsulates ?

Quote
>Since Read(F, ...) and Write(F, ...) don't actually read/write to the
>disc, but to a buffer, ISTM that it should be practical to read/write to
>the string itself.

I think Ray Lischner does something like this in a helper technique in "Secrets
of Delphi 2" to write RTTI to a TMemo. I'm not sure I _really_ understand what
he's doing though <g>.

Alan Lloyd
alangll...@aol.com

Re:Extract numbers from strings


JRS:  In article <20000406162526.24686.00000...@nso-fz.aol.com> of Thu,
6 Apr 2000 20:25:26 seen in news:alt.comp.lang.borland-delphi,

Quote
AlanGLLoyd <alangll...@aol.com> wrote:
>In article <sTHHojBaEK74E...@merlyn.demon.co.uk>, Dr John Stockton
><j...@merlyn.demon.co.uk> writes:

>>        AssignStr(var F; String) ;
>>where the string, rather than holding the name of the data, holds the
>>data itself.

>Isn't that effectively what a TStringStream encapsulates ?

It looks as if it might well be; there's also TMemoryStream.

At a first look, they possess some of the features; but omit the
advantage of using exactly the same code.  It's been a nice feature of
Pascal that a program can, almost by default, be designed to write to a
file, or a device with a user-supplied driver, or a device (including
NUL) with a DOS-supplied driver - and only the Assign parameter needs to
be changed.

The D3 help on function TStringStream.Write does not actually explain
what the return value is; though it seems that only one possibility is
likely.

However, as there is apparently no provision for doing anything like
Read(F, X1, X2, X3) or Write(F, Y1:9:4, S1:30) in one statement, ISTM
that these streams, doubtless valuable, are neither truly equivalent to
my suggestion, nor an effective substitute.

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
 <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
 <URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ;
 <URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ.

Re:Extract numbers from strings


In article <xVRgJ+CDuc74E...@merlyn.demon.co.uk>, Dr John Stockton

Quote
<j...@merlyn.demon.co.uk> writes:
>The D3 help on function TStringStream.Write does not actually explain
>what the return value is; though it seems that only one possibility is
>likely.

>However, as there is apparently no provision for doing anything like
>Read(F, X1, X2, X3) or Write(F, Y1:9:4, S1:30) in one statement, ISTM
>that these streams, doubtless valuable, are neither truly equivalent to
>my suggestion, nor an effective substitute.

TStream's Read and Write methods are abstract, so you could write a method
which does what you want for a descendant of TStream. The major stumbling block
to "equivalent code structure" ISTM is the unusual syntax of the file Read and
Write statements which have an indeterminate number of parameters but are not
open arrays.

Alan Lloyd
alangll...@aol.com

Re:Extract numbers from strings


JRS:  In article <20000407164237.19068.00000...@nso-fg.aol.com> of Fri,
7 Apr 2000 20:42:37 seen in news:alt.comp.lang.borland-delphi,

Quote
AlanGLLoyd <alangll...@aol.com> wrote:
>In article <xVRgJ+CDuc74E...@merlyn.demon.co.uk>, Dr John Stockton
><j...@merlyn.demon.co.uk> writes:

>>The D3 help on function TStringStream.Write does not actually explain
>>what the return value is; though it seems that only one possibility is
>>likely.

>>However, as there is apparently no provision for doing anything like
>>Read(F, X1, X2, X3) or Write(F, Y1:9:4, S1:30) in one statement, ISTM
>>that these streams, doubtless valuable, are neither truly equivalent to
>>my suggestion, nor an effective substitute.

>TStream's Read and Write methods are abstract, so you could write a method
>which does what you want for a descendant of TStream. The major stumbling block
>to "equivalent code structure" ISTM is the unusual syntax of the file Read and
>Write statements which have an indeterminate number of parameters but are not
>open arrays.

Plus the X:y:z structure, which is most useful on output (and X:y could
be used on input, with obvious meaning, if the compiler writers so
chose).

Yes, a language can be extended for many purposes by its users, and each
purpose can be achieved in many ways (with many possible defects).  If
such were done by the compiler writers, there would be one popular well-
tested way, without error, making programs more easily readable by
others; and the freedom to do one's own thing at need would remain.

Wirth's Pascal was a simple language containing little more than needed
- for example, the trig function set is almost optimally minimal, though
cos is inessential and atan2 would have been better than arctan - Delphi
is very much richer, sometimes making it tricky IMHO to see the wood for
the trees.

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
 <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
 <URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ;
 <URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ.

Other Threads