TStrings delimited text to variant array

I need a function to create a variant array from a delimited text

anybody got this in their repository ?

i know this looks lazy, but i'm feeling like that too this morning... :)

function DelimTextToVariant(List: TStrings): Variant;
begin
  //
end;

Open in new window

LVL 39
Geert GOracle dbaAsked:
Who is Participating?
 
ThommyCommented:
Hi Geert,

it must go somehow like this.

Sorry, have had no time to test it...
function DelimTextToVariant(List: TStrings): Variant;
var
  i:integer;
begin
  result:=VarArrayCreate([0,List.count-1],varVariant);
  For i := 0 To List.Count-1 do begin
    result[i]:=List[i];
  end;
end;

Open in new window

0
 
jimyXCommented:
Here is some code, let's see if it helps:
var
  arr:array of variant;
procedure DelimTextToVariant(List:TStrings;var a: array of Variant);
var
  i:integer;
begin
  for i := 0 to list.Count-1 do
    begin
      a[i] := list.Strings[i];
    end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  s:Tstrings;
begin
  s:=Tstringlist.Create;
  s.Delimiter := ',';
  s.DelimitedText := 'A,B,1,True,#';
  showmessage(s.Text);
  SetLength(arr,s.Count);
  DelimTextToVariant(s, arr);
  showmessage(arr[0]);
  showmessage(arr[3]);
  showmessage(arr[s.Count-1]);
  s.Free;
end;

Open in new window


I know great experts feel lazy when it comes to handle simple issue.
If you want a tough issue, to loose the laziness feeling, let me know :-)
0
 
RezaSadighCommented:
Hi my friend,
OK here you are:

Good Luck
Reza
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
RezaSadighCommented:

type
  TVAry= array of Variant;
function DelimTextToVariant(List: TStrings): TVAry;
var
  i: Integer;
begin
  SetLength(Result, List.Count);
  for i:=0 to List.Count-1 do
    Result[i]:= List[i];
end;

Open in new window

0
 
RezaSadighCommented:
sorry for broken Comment !!!
0
 
Geert GOracle dbaAuthor Commented:
>jimyX
looks like your just as lazy as i was
> in test, no try finally, no indents
> you made a procedure with a var variable instead of using the function
if somebody asks you to get apples from the market,
why do you insist on bringing back oranges ? (procedure <> function)

>RezaSadigh
i asked for a variant as output in my function, not array of variant

>jimyX, RezaSadigh
neither of you commented on the change to procedure or a different type of return variable
why would you want to change this anyway ?

please stick to what is requested
0
 
Geert GOracle dbaAuthor Commented:
Thommy,
i used your function ... somewhat

i didn't specify deleting empty strings, that's something i bumped into afterwards
i also used a separate variable V and varOleStr instead of varVariant

 
function DelimTextToVariant(List: TStrings): Variant;
var V: Variant;
  I: Integer;
begin
  V := Null;
  for I := List.Count-1 downto 0 do
    if List[I] = '' then
      List.Delete(I);
  if List.Count = 1 then
    V := List.Text
  else if List.Count > 1 then
  begin
    V := VarArrayCreate([0, List.Count-1], varOleStr);
    for I := 0 to List.Count - 1 do
      V[I] := List[I];
  end;
  Result := V;
end;

Open in new window

0
 
ThommyCommented:
Geert, thank you for the points!!!
:o)))
0
 
Geert GOracle dbaAuthor Commented:
Lol, you deserved them.

It does make it more difficult for me to keep up with you :)
0
 
ThommyCommented:
I will not have the time to keep this level the next months, because I have really a lot of projects to do!!!

So it should be no problem for you to catch up with me... ;o))
0
 
Geert GOracle dbaAuthor Commented:
lol, same here, i just got 60 days of work for next month ...
it's a good thing, they didn't plan for the nights yet
0
 
jimyXCommented:
>   "looks like your just as lazy as i was"
No Geert I wasn't, sorry to disappoint you, that was my repository that you asked for.

I normally work on the author's requirement, and yours was:
   "I need a function to create a variant array from a delimited text"
So I offered my code and clearly told you "let's see if it helps". So if you did not like it or found no help of using it, it's up to you. At the end it's your party.

And BTW, you are welcome.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.