Recent

Author Topic: Huge string problem!!! Can lazarus fix this?  (Read 2761 times)

Robert W.B.

  • Sr. Member
  • ****
  • Posts: 328
  • Love my Wife, My Kids and Lazarus/Freepascal.
Huge string problem!!! Can lazarus fix this?
« on: February 02, 2016, 06:21:39 pm »
Hi Everyone.
I have a string with numbers,  that I want to get into an array? The string have a space between the numbers. Is it possible, to get this into an array in Lazarus?

MyString='6 12 23 25 30 32 39 41 50 0 0'; converted into MyArr?

MyArr[1]:='6';
MyArr[2]:='12';
MyArr[3]:='23';
MyArr[4]:='25';
MyArr[5]:='30';
MyArr[6]:='32';
MyArr[7]:='39';
MyArr[8]:='41';
MyArr[9]:='50';
MyArr[10]:='0';
MyArr[11]:='0';

Thanks in advance
Bob
Rob

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: Huge string problem!!! Can lazarus fix this?
« Reply #1 on: February 02, 2016, 06:40:42 pm »
Must it be an array?
Why not a TStringList, it's default property is indexed as well?
Use DelimitedText to set the text.

Code: [Select]
var
  SL: TStringList;
  i: Integer;
begin
  SL := TStringList.Create;
  try
    SL.Delimiter := #32;
    SL.StrictDelimiter := True;
    SL.DelimitedText := MyHugeString;
    //do stuff
    for i := o to SL.Count - 1 do
      writeln('SL[',i,'] = ',SL[i]);
  finally
    SL.Free;
  end;
end;

Bart

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Huge string problem!!! Can lazarus fix this?
« Reply #2 on: February 02, 2016, 06:47:36 pm »
If it has to be an array, you can go via a stringlist. Something like this:

Code: Pascal  [Select][+][-]
  1. uses classes, types;
  2.  
  3. function Str2Array(const aSpaceDelimitedString: string): TStringDynArray;
  4. var
  5.   sl: TStringList;
  6.   i: integer;
  7. begin
  8.   if (aSpaceDelimitedString = '') then
  9.     SetLength(Result, 0)
  10.   else begin
  11.     sl:=TStringList.Create;
  12.     try
  13.       sl.Delimiter:=' ';
  14.       sl.StrictDelimiter:=True;
  15.       sl.DelimitedText:=aSpaceDelimitedString;
  16.       SetLength(Result, sl.Count);
  17.       for i:=0 to sl.Count-1 do
  18.         Result[i]:=sl[i];
  19.     finally
  20.       sl.Free;
  21.     end;
  22.   end;
  23. end;    

Robert W.B.

  • Sr. Member
  • ****
  • Posts: 328
  • Love my Wife, My Kids and Lazarus/Freepascal.
Re: Huge string problem!!! Can lazarus fix this?
« Reply #3 on: February 02, 2016, 07:22:24 pm »
Hi Bart and HowardPC. I lokked at HowardPC's solution do I need an array but, I can't get it!

I understand I need to use the function Str2Array and call it by Str2Array but, where do I put MyString in the code?

Thanks in advance
Bob
Rob

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Huge string problem!!! Can lazarus fix this?
« Reply #4 on: February 02, 2016, 07:43:04 pm »
something like:

Code: Pascal  [Select][+][-]
  1. ...
  2.  
  3. uses types, classes, ...;
  4.  
  5. const
  6.    MyString='6 12 23 25 30 32 39 41 50 0 0';
  7.  
  8. var
  9.   MyArray: TStringDynArray;
  10.  
  11. begin
  12.   MyArray:=Str2Array(MyString);
  13.   // use MyArray here
  14.  ...

Robert W.B.

  • Sr. Member
  • ****
  • Posts: 328
  • Love my Wife, My Kids and Lazarus/Freepascal.
Re: Huge string problem!!! Can lazarus fix this?
« Reply #5 on: February 02, 2016, 08:00:54 pm »
Great HowardPC!!! It works like a charm. Fantastic. I'm so happy :D
Take care and many hugs from Bob.
Rob

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Huge string problem!!! Can lazarus fix this?
« Reply #6 on: February 07, 2016, 09:54:37 am »
Actually fpc 3.0.0 (and I think later 2.7.x) have support for dynamic array constructors. So you can simply write:

Code: Pascal  [Select][+][-]
  1. var
  2.   MyArray: TStringDynArray;
  3.  
  4. begin
  5.   MyArray.Create('6','12','23','25','30','32','39','41','50','0','0');
  6. ...
  7.  

Note that the IDE CodeTools does not yet fully support this construct. 
 

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: Huge string problem!!! Can lazarus fix this?
« Reply #7 on: February 07, 2016, 01:37:39 pm »
Uhmm, did came to mind. Probably rubbish but it *should* work:
Code: Pascal  [Select][+][-]
  1. program convarray;
  2. {$APPTYPE CONSOLE}
  3. Type
  4.   TCharArray = array[0..65535] of ansichar;
  5. function GetMeChars(const a: AnsiString):TCharArray;
  6. var
  7.   r:TCharArray absolute a;
  8. begin
  9.   Result := r;
  10. end;
  11. begin
  12.   { did not test this ;) }
  13. end.
  14.  
  15.  
« Last Edit: February 07, 2016, 01:39:20 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018