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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

RezaSadighCommented:
sorry for broken Comment !!!
0
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.