Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting Data Out Of Columns 1

Status
Not open for further replies.

PaidtheUmpire

Programmer
Jan 4, 2004
105
0
0
AU
Hey,

I have data in the following format, in a MEMO box.

10-10-2005 05:00:00, 125.001, -15.152, 0
10-10-2005 09:00:00, 125.486, -15.152, 0
10-10-2005 13:00:00, 125.123, -15.182, 0...

Is there a way of easily getting out the data from a row, to calculate the high and low values in each "column"

I would like to get at the data like so:

For a := 1 to NoLines do
begin
..............
end;

Thanks for your help

Delphi, Delphi, Delphi. Oi! Oi! Oi!
 
Do you write the data into the Memo, or read it in from a file? Your data looks like a Comma Separated Value (CSV) format file.
If your reading it in, a very simple way to extract the data is use a [tt]READLN(fInFile,Var1,Var2,Var3,Var4.....varN);[/tt]
This will require you to know the types of each variable.
Alternatively, you can place the data in a table, and use ADO to read a CSV files - you'll need an [tt]ADOconnection[/tt], and either an [tt]ADOtable[/tt] or [tt]ADOquery[/tt].
You should use the [tt]MS DB Provider for ODBC drivers[/tt], and build the connection string.

If the Memo data is not from a file, you'll need to write a text parser for your line by line check using the [tt]COPY[/tt] function.
Something like:
Code:
  for I := 0 to (Memo1.Lines.Count - 1) do begin
    sCurrLine := Memo1.Lines.Strings[I]; // current line to play with
    nComma := PosEx(',',sCurrLine,1);  // first comma
    nVar1 := copy(Memo1.Lines.Strings[I], 1, nComma - 1); // first string
    nLastComma := nComma;
    nComma := PosEx(',',sCurrLine,nComma + 1); // next comma
    nVar2 := copy(Memo1.Lines.Strings[I], LastComma + 1,nComma - 1);
    nLastComma := nComma;
    nComma := PosEx(',',sCurrLine,nComma + 1); // next comma
    nVar3 := copy(Memo1.Lines.Strings[I], LastComma + 1,nComma - 1);
    // etc etc etc
  end;
You could embed a lot of this as functions or objects.

Perhaps you could be a bit more specific in what your after....

Cheers,
Chris [pc2]
 
Oops - forgot about your max/min question!
If your data is in an array, then you can use the [tt]MaxValue[/tt] and [tt]MinValue[/tt] functions (in the Math unit)
From the help file:
[tt]function MaxValue(const Data: array of Double): Double;[/tt]
There are also MaxInt and MinInt functions for integer arrays.

However, how you put them in the array depends on your data source - the memo or a data file. Using the parser I outlined before you could have an array of records for this purpose.



Cheers,
Chris [pc2]
 
Thanks for that,

I'm having a bit of trouble with your code though, could you see if you find what i'm doing wrong.

Code:
  sCurrLine := Memo1.Lines.Strings[a]; // current line to play with
  nComma := PosEx(',',sCurrLine,1);
  Column1 := copy(Memo1.Lines.Strings[a], 1, nComma - 1); // first string
  nLastComma := nComma;
  nComma := PosEx(',',sCurrLine,nComma + 1); // next comma
  Column2 := copy(Memo1.Lines.Strings[a], nLastComma + 1,nComma - 1);
  nLastComma := nComma;
  nComma := PosEx(',',sCurrLine,nComma + 1); // next comma
  Column3 := copy(Memo1.Lines.Strings[a], nLastComma + 1,nComma - 1);

What is happening is that Column1 is fine but Column2 is getting both Column2 and Column3 information into it and Column3 is not getting anything.

Thanks for your help so far though...

Delphi, Delphi, Delphi. Oi! Oi! Oi!
 
Sorry about that - my fault for writing it off the top of my head!
Here's a tested working version [tongue]:
Code:
  var
    sCurrLine,
    nVar1, nVar2, nVar3, nVar4 : string;
    I, nComma, nLastComma : integer;
begin
  for I := 0 to (Memo1.Lines.Count - 1) do begin
    // initialise the temporary variables
    nLastComma := 0; // for where we left off

    sCurrLine := Memo1.Lines.Strings[I]; // current line to play with
    nComma := PosEx(',',sCurrLine,1);  // first comma location - 0 if not found
    if nComma > 0 then  // comma exists, so extract it
      nVar1 := copy(Memo1.Lines.Strings[I], 1, nComma - 1) // first string
    else  // no comma, so get the whole line
      nVar1 := copy(Memo1.Lines.Strings[I], 1, length(sCurrLine));
    nLastComma := nComma;

    nComma := PosEx(',',sCurrLine,nComma + 1); // next comma
    if nComma > 0 then
      nVar2 := copy(Memo1.Lines.Strings[I], nLastComma + 1, nComma - nLastComma -1)
    else
      // rest of line past the last comma - this must always be here for your last variable
      nVar2 := copy(Memo1.Lines.Strings[I], nLastComma + 1, length(sCurrLine) - nLastComma); 
    nLastComma := nComma;

    nComma := PosEx(',',sCurrLine,nComma + 1); // next comma
    if nComma > 0 then
      nVar3 := copy(Memo1.Lines.Strings[I], nLastComma + 1, nComma - nLastComma -1)
    else
      nVar3 := copy(Memo1.Lines.Strings[I], nLastComma + 1, length(sCurrLine) - nLastComma); // rest of line
    nLastComma := nComma;

    nComma := PosEx(',',sCurrLine,nComma + 1); // next comma
    if nComma > 0 then
      nVar4 := copy(Memo1.Lines.Strings[I], nLastComma + 1, nComma - nLastComma -1)
    else
      nVar4 := copy(Memo1.Lines.Strings[I], nLastComma + 1, length(sCurrLine) - nLastComma); // rest of line

    // etc etc etc if you have more variables!
  end; // if
end; //procedure/function

The syntax for PosEx:
[tt]function PosEx(const SubStr, S: string; Offset: Cardinal = 1): Integer;[/tt]
where:
[ul]
[li][tt]SubStr[/tt] is what your seeking [/li]
[li][tt]S[/tt] is your string [/li]
[li][tt]Offset[/tt] is where you start searching in the string (default is 1)[/li]
[/ul]

The syntax for copy:
[tt]function Copy(S; Index, Count: Integer): string;[/tt]
where:
[ul]
[li][tt]S[/tt] is your string [/li]
[li][tt]Index[/tt] is where you start in the string (default is 1)[/li]
[li][tt]Count[/tt] is how many characters to extract[/li]
[/ul]

Hope that works for you now!

Cheers,
Chris [pc2]
 
Thanks,

I found the mistake and this post just confirmed what i thought it was. Thanks anyways.

:p

PTU

Delphi, Delphi, Delphi. Oi! Oi! Oi!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top