Board index » delphi » Did I say something wrong??

Did I say something wrong??

I guess I scared everyone off with the perl regex - as I haven't even been
acknowledged yet. (Even a welcome to the world of Delphi would have been nice)

Let's try a different question this time.

In Object Pascal:
After having read in a line from a configuration file and stored that line to a
variable (I have made it this far) ---- how does one split the string on a
special character to store each portion into different variables.

Jack

 

Re:Did I say something wrong??


On Sat, 16 Nov 2002 08:12:50 GMT, "Jack D" <goodcal...@hotmail.com>
wrote:

Quote
>I guess I scared everyone off with the perl regex - as I haven't even been
>acknowledged yet. (Even a welcome to the world of Delphi would have been nice)

>Let's try a different question this time.

>In Object Pascal:
>After having read in a line from a configuration file and stored that line to a
>variable (I have made it this far) ---- how does one split the string on a
>special character to store each portion into different variables.

>Jack

Hi Jack,

I didn't see any previous post from you. Maybe the news server ate it.

Anyway:

procedure ChopStr(s: String; var s1, s2: string; ch: Char);
// Chop at first instance of ch. Return two parts with ch removed.
var
  i: Integer;
begin
    s1 := '';
    s2 := '';
    i := Pos(ch, s);
    if i > 0 then begin
      s1 := LeftStr(s, Pred(i));
      s2 := RightStr(s, Length(s) - i);
    end; {if i > 0}
end;

HTH

************************************************************
Ian Jennings
Microware Data Services Limited
This post is made entirely from recycled ones and noughts
************************************************************

Re:Did I say something wrong??


On Sat, 16 Nov 2002 08:12:50 GMT, "Jack D" <goodcal...@hotmail.com>
wrote:

Quote
>In Object Pascal:
>After having read in a line from a configuration file and stored that line to a
>variable (I have made it this far) ---- how does one split the string on a
>special character to store each portion into different variables.

>Jack

Whoops, big{*word*19}-up - forgot the other bits. Hey ho.

function LeftStr(const strIn: string; n: Integer): string;
// Return leftmost n characters from strIn
begin
    if n <= Length(strIn) then
      Result := Copy(strIn, 1, n)
    else
      Result := StrIn;
end;

function RightStr(const strIn: string; n: Integer): string;
// Return the rightmost n characters from strIn
begin
    if n < Length(strIn) then
      Result := Copy(strIn, Succ(Length(strIn)) - n, n)
    else
      Result := strIn;
end;

************************************************************
Ian Jennings
Microware Data Services Limited
This post is made entirely from recycled ones and noughts
************************************************************

Re:Did I say something wrong??


Quote
"Ian Jennings" <i...@microwaredata.co.uk> wrote in message

news:5vvbtu0s2fkv9nddqrl6ijhhnlhfbi09cp@4ax.com...

[..snip..]

Quote
> function RightStr(const strIn: string; n: Integer): string;
> // Return the rightmost n characters from strIn
> begin
>     if n < Length(strIn) then
>       Result := Copy(strIn, Succ(Length(strIn)) - n, n)
>     else
>       Result := strIn;
> end;

Thank you so much. I will incorporate this immediately.

I was hoping to use a regular expression (coming from a perl background - I
would feel more 'at home'). Hence I had downloaded TRegExpr. Does anyone have
experience with this module?

Also - now that I have someone's attention. Which group is best for newcomers to
post to?

Jack

Re:Did I say something wrong??


Quote
"Jack D" <goodcal...@hotmail.com> wrote in message

news:66nB9.5743$bh1.487871@news1.telusplanet.net...
Quote
> I guess I scared everyone off with the perl regex - as I haven't even been
> acknowledged yet. (Even a welcome to the world of Delphi would have been

nice)

Typically when one doesn't get an answer its because no one has one.

Quote
> After having read in a line from a configuration file and stored that line
to a
> variable (I have made it this far) ---- how does one split the string on a
> special character to store each portion into different variables.

Delphi has a number of "string handling routines" documented in the help. To
answer your question

i := Pos (theSpecialCharacter, theString);
leftSide := Copy (theString, 1, i - 1);
rightSide := Copy (theString, i + 1, MaxInt);

Re:Did I say something wrong??


Quote
"Jack D" <goodcal...@hotmail.com> wrote in message news:%pnB9.5877
> I was hoping to use a regular expression (coming from a perl background -
I
> would feel more 'at home'). Hence I had downloaded TRegExpr. Does anyone
have
> experience with this module?

From where did you get the component?

Re:Did I say something wrong??


Quote
"Bruce Roberts" <b...@bounceitattcanada.xnet> wrote in message

news:atnB9.18444$H67.82607@tor-nn1.netcom.ca...

Quote

> "Jack D" <goodcal...@hotmail.com> wrote in message news:%pnB9.5877

> > I was hoping to use a regular expression (coming from a perl background -
> I
> > would feel more 'at home'). Hence I had downloaded TRegExpr. Does anyone
> have
> > experience with this module?

> From where did you get the component?

From http://anso.virtualave.net/ -- it seems to do what I need. But I don't know
how to get at the stored variables after the regex has finished.

Re:Did I say something wrong??


Quote
"Jack D" <goodcal...@hotmail.com> wrote in message

news:66nB9.5743$bh1.487871@news1.telusplanet.net...

Quote
> I guess I scared everyone off with the perl regex - as I haven't even been
> acknowledged yet. (Even a welcome to the world of Delphi would have been nice)

The reason why you didn't get an answer from ME is this line:
$foo=~/(.*)\~(.*)/;
for which I have no idea what it does :)
Also, I've never used TRegExp

Btw. Welcome to the wonderful world of Delphi!!! :-D

Quote
> In Object Pascal:
> After having read in a line from a configuration file and stored that line to a
> variable (I have made it this far) ---- how does one split the string on a
> special character to store each portion into different variables.

And what is that special character? For example, if you want to split the string on a specified
character, then you can do it like this:

// text - your text, chr - special character, text1,text2 - results

procedure SplitTextOnChar(text: string; chr: char; var text1, text2: string);
var
 num: integer;   // counter, moves from character to character
 ok: boolean;
begin
 ok := false;
 num := 1;
 text1 := '';
 text2 := '';
 repeat
  text1 := text1 + Copy(text,num,1);  // copies the char to the first text
  if Copy(text,num,1) = chr then begin
   ok := True;   // when the special character is found, it puts the rest to text2
   text2 := Copy(text,num+1,Length(text)-num)
   Delete(text1,num,1);   // delete the special character from text1
  end;
  inc(num)   // increase the counter
 until ok
end;

OK, I guess you could write this a lot shorter and simpler (hadn't have time to improvise), but this
way it will work for sure. For example, if you type

SplitTextOnChar('Monkeys are~cool animals','~',txt1,txt2);

then txt1='Monkeys are';
txt2='cool animals';

It would not be hard now to write a procedure that would split the text into n-parts, but I will
leave that to you :) I guess I would use arrays to do that, or merge the procedure into the program
and skip the array part.
Also I'm not sure if there already is a procedure in Delphi to deal with a problem like this, but I
think there isn't. I suggest you check out the Delphi Help for the Copy, Insert, Length and Concat
functions (VCL).

Hope this helped, Csaba

Re:Did I say something wrong??


In article <66nB9.5743$bh1.487...@news1.telusplanet.net>, "Jack D"

Quote
<goodcal...@hotmail.com> writes:
>After having read in a line from a configuration file and stored that line to
>a
>variable (I have made it this far) ---- how does one split the string on a
>special character to store each portion into different variables.

There are three basic techniques (AFAIK) ...

1 Split the string using Pos() (on the separator) until the end of the string
is reached.

2 Treat the string as an array, copying to another string (preferably as an
array) until the separator is reached, then copying to another string etc.

3a Use a TStringList. If your separator is a comma and there are no spaces in
the individual parts, then assign the string to the CommaText of a TStringList.
This automatically splits the elements to the differentStrings[] of the
TStringList.

3b Use a TStringList plus. If you have spaces and commas in the elements, and
use another character as the separator, then treating the string as an array,
change every comma and space to other unused characters (maybe non-ASCII). Then
change the separator to a comma, put the string in a TStringlist as CommaText,
then change your space and comma replacements back to the original in the
TStringList.Text (not CommaText). This sounds very complex but it is simple
code (only five three-line "for" + "if" loops, and two or three assigns), and
makes for clarity in your code. (and it's what I'd rather do in this case).

Alan Lloyd
alangll...@aol.com

Re:Did I say something wrong??


Quote
"Jack D" <goodcal...@hotmail.com> wrote in message news:66nB9.5743
> variable (I have made it this far) ---- how does one split the string on a
> special character to store each portion into different variables.

May be you could use my function 'Piece' here. F.ex. you have a string
Name := 'John Fitzgerald Kennedy'
then you can extract words from it
FirstName:= Piece(Name,' ',1,1);  (='John')
FirstNames:= Piece(Name,' ',1,2);   (= 'John Fitzgerald')
SurName:=Piece(Name,' ',3,1);   (='Kennedy')
The delimiter may be anything, a character, punctuation or a word etc.
- Matti
(********************************************)
(*  Function 'Piece'    by M.Jokinen                                  *)
(********************************************)
function Piece(str,delimiter:string; start,count:integer): string;
var d,k :integer;
 Begin
   Result:='';
   While (start>1) and (Pos(delimiter,str) > 0) do begin
      Delete(str,1,Pos(delimiter,str)+Length(delimiter)-1);
      dec(start);
   end;
   If start=1 then begin
    while (count>0) and (Pos(delimiter,str) > 0) do begin
     Result:=Result+ Copy(str,1,Pos(delimiter,str)-1)+delimiter;
     Delete(str,1,Pos(delimiter,str)+Length(delimiter)-1);
     dec(count);
    end;
    if count>0 then Result:=Result+str;
   end;
  k:=length(Result);
  d:=length(delimiter);
  if k>0 then if Copy(Result,k-d+1,k) = delimiter then
Delete(Result,k-d+1,k);
End;

Re:Did I say something wrong??


Quote
"Jack D" <goodcal...@hotmail.com> wrote in message

news:66nB9.5743$bh1.487871@news1.telusplanet.net...

Quote
> I guess I scared everyone off with the perl regex - as I haven't even been
> acknowledged yet. (Even a welcome to the world of Delphi would have been nice)

> Let's try a different question this time.

> In Object Pascal:
> After having read in a line from a configuration file and stored that line to
a
> variable (I have made it this far) ---- how does one split the string on a
> special character to store each portion into different variables.

Wow! I now have a number of options. Thank you.

It seems though, that the language itself doesn't support what I would consider
to be a simple task. i.e. some sort of 'split' function. From the answers so
far, it seems that everyone writes their own procedures or functions for such a
thing. I assumed (??) that the language would easily support this functionality.

Regular Expressions are a powerful thing! Something I can't do without. So,
while I now have a temporary solution to this particular problem, I will
continue to battle with this TRegExpr package.

Thanks again to all.

Jack
PS..BTW this perl one-liner does this task automatically without a regex :)

@array = split(/\~/,$string);

Re:Did I say something wrong??


Quote
"Jack D" <goodcal...@hotmail.com> wrote in message

news:66nB9.5743$bh1.487871@news1.telusplanet.net...

Quote
> I guess I scared everyone off with the perl regex - as I haven't even been
> acknowledged yet. (Even a welcome to the world of Delphi would have been nice)

> Let's try a different question this time.

> In Object Pascal:
> After having read in a line from a configuration file and stored that line to
a
> variable (I have made it this far) ---- how does one split the string on a
> special character to store each portion into different variables.

Wow! I now have a number of options. Thank you.

It seems though, that the language itself doesn't support what I would consider
to be a simple task. i.e. some sort of 'split' function. From the answers so
far, it seems that everyone writes their own procedures or functions for such a
thing. I assumed (??) that the language would easily support this functionality.

Regular Expressions are a powerful thing! Something I can't do without. So,
while I now have a temporary solution to this particular problem, I will
continue to battle with this TRegExpr package.

Thanks again to all.

Jack
PS..BTW this perl one-liner does this task automatically without a regex :)

@array = split(/\~/,$string);

Re:Did I say something wrong??


Quote
"Jack D" <goodcal...@hotmail.com> wrote in message news:jCnB9.6015
> From http://anso.virtualave.net/ -- it seems to do what I need. But I
don't know
> how to get at the stored variables after the regex has finished.

I took a quick look at the on-line documentation. The unit has a
SplitRegExpr procedure. Also the tRegExp class has a Split method.

Re:Did I say something wrong??


Quote
"Jack D" <goodcal...@hotmail.com> wrote in message news:jCnB9.6015
> From http://anso.virtualave.net/ -- it seems to do what I need. But I
don't know
> how to get at the stored variables after the regex has finished.

I took a quick look at the on-line documentation. The unit has a
SplitRegExpr procedure. Also the tRegExp class has a Split method.

Re:Did I say something wrong??


Quote
Jack D wrote in message <1jxB9.8340$bh1.593...@news1.telusplanet.net>...

[...]

Quote
>It seems though, that the language itself doesn't support what I would
consider
>to be a simple task. i.e. some sort of 'split' function. (...)
>                     ...this TRegExpr package.

You are confusing the language (which should supply data types)
with the run-time library (which should supply ready-made code).

The run-time library is extensible: you can add your own library
code - but if nobody has written the exact code you want, it's
up to you to do so.

Delphi gives you all the building blocks to build your own split
function. Or an entire regular expressions package, and in fact
somebody already did so. That you consider it incomplete, or
poorly documented, is between you and him.

Perl and Pascal are very different languages. Perl is big on
regular expressions. Pascal is not. That simple. It's strengths
are in other areas. If what you want to do is better done in
Perl (which is possible), you should by all means do it in Perl.

When doing things in Delphi, it's best to do them the Delphi way.
We have seen a thousand posts here of people who complain about
poor PChar support. Well, that figures. String handling in Delphi
is geared towards Delphi strings. PChars are a C compatibility
feature. You may need them because Windows is C-oriented, but
doing anything with PChars except copying them to and from strings
is misguided.

It's happened before that somebody asked "When I do <this>, why
is it so hard?" and the answer was: because that's not how it's
done in Delphi. Tell us what your program does (_what_, not _how_),
and we'll be happy to explain how we'd solve it. In real life,
you'd know when to use a hammer and when to use an axe. Knowing
when to use Delphi and when to stick to Perl is no different.
Well, not much.

(BTW, the answer is of course that against plate armour you'd use
a hammer, and against most everything else the axe.)

Groetjes,
Maarten Wiltink

Go to page: [1] [2]

Other Threads