Create an array/list and and use a for loop to print in informix - informix

Create an integer or float array and print the elements using for loop
in a procedure/function in Informix
I have referred to the info centre and almost googled but couldn't be able to find it out:
CREATE PROCEDURE main()
begin
#SET #myArrayOfValue := '2,5,2,23,6,';
#print the elements
end;
end procedure
define an array of elements either in procedure or function.
perform some looping and print.

Related

How to interpret arrays passed from VBscript to a Delphi COM server App

I am trying to pass an array of bytes from VBscript to my windows Delphi Application and can't seem to find the correct syntax to interpret the passed data.
The requirement is fairly simple as the VBscript snippet below demonstrates
Dim inst,arr(5)
Sub Main
set inst=instruments.Find("EP1")
arr(0) = 0
arr(1) = 1
arr(2) = 2
arr(3) = 3
arr(4) = 4
inst.writebytes arr,5
end Sub
I can get the server to accept the olevariant passed by the script but the data seems garbled, my example server code is shown below and is based on the Stackoverflow question here How to use variant arrays in Delphi
procedure TInstrument.WriteBytes(Data: OleVariant; Length: Integer);
var i,n:integer; Pdat:Pbyte; Adata:PvarArray;
begin
if VarIsArray(data) then
begin
n:=TVarData(Data).VArray^.Bounds[0].ElementCount;
Adata:= VarArrayLock(Data);
Getmem(Pdat,length);
try
for i:=0 to length-1 do
Pdat[i]:=integerArray(Adata.data^)[i];
Finstrument.WriteBytes(Pdat,Length);
finally
freemem(Pdat)
end;
end;
end;
So the idea is to accept the integers passed by the script, convert it to the local data representation (array of byte) then pass it on to my function to use the data.
I have tried several different data types and methods to try and get some ungarbled data out of the variant all to no avail.
What is the correct method of extracting the array data from the passed variant?
Also, TVarData(Data).VArray^.Bounds[0].ElementCount has a value of zero, why would that be?
Arrays created in VBScript are
zero based
untyped
declared with upper bound (not size as you assumed; size of array declared as Dim arr(5) is 6)
include dimension info in them (so you don't need to pass it along with the array)
When used in COM, they are passed as variant arrays of type varVariant (as the Ondrej Kelle points out in his comment). To process such an array in your method you have to assert that:
the value is a single dimensional array
each element can be converted to byte
You can write helper routine for that:
function ToBytes(const Data: Variant): TBytes;
var
Index, LowBound, HighBound: Integer;
ArrayData: Pointer;
begin
if not VarIsArray(Data) then
raise EArgumentException.Create('Variant array expected.');
if VarArrayDimCount(Data) <> 1 then
raise EArgumentException.Create('Single dimensional variant array expected.');
LowBound := VarArrayLowBound(Data, 1);
HighBound := VarArrayHighBound(Data, 1);
SetLength(Result, HighBound - LowBound + 1);
if TVarData(Data).VType = varArray or varByte then
begin
ArrayData := VarArrayLock(Data);
try
Move(ArrayData^, Result[0], Length(Result));
finally
VarArrayUnlock(Data);
end;
end
else
begin
for Index := LowBound to HighBound do
Result[Index - LowBound] := Data[Index];
end;
end;
for loop in the routine will be horribly slow when processing large arrays, so there's optimization for special case (variant array of bytes) that uses Move to copy bytes to result. But this will never happen with VBScript array. You might consider using VB.Net or PowerShell.
Using such a routine has downside of keeping 2 instances of the array in memory - as variant array and as byte array. Use it as a guide when applying it to your use case.

pass list of values as input parameter to PL/SQL procedure

Hi I have a requirement where I get list of values to a input parameter in PL/SQL procedure. The size of the input list varies which is dynamic. How to handle this requirement any help?
CREATE OR REPLACE PACKAGE PKG_TEST AS
TYPE X IS TABLE OF VARCHAR2(30);
PROCEDURE XYZ(Y IN X);
END PKG_TEST;
/
The type can be declared as "TABLE OF" OR "VARRAY(10)";
CREATE OR REPLACE PACKAGE BODY PKG_TEST AS
PROCEDURE XYZ(Y IN X) AS
BEGIN
FOR I IN Y.FIRST..Y.LAST
LOOP
DBMS_OUTPUT.PUT_LINE('THE VALUE OF I IS'||Y(I));
END LOOP;
END;
END PKG_TEST;
/
DECLARE
BEGIN
PKG_TEST.XYZ('1','2','3','4');
END;
/
You could use a varchar parameter in sql, each value must be separated by a comma, something like this:
'value1,value2,value3,value4,...,'
So, you can read the values using the function split of sql
I hope that I understood your question

Inserting element in a linked list with Pascal

I've seen some algorithms designed to append an elment at the end of a linked list here and browsing other website, then I wrote a small procedure that i believe it should append a given element at the end of the list but it doesn't seems to work.
My question here is, why it doesn't work?
I defined pointers and nodes as follows:
Pointer = ^Node;
Node = record
about : element;
next : Pointer;
end;
And the following procedure receives a linked list L and a q element that should be appended to the end of L
First I define the record that I'll insert afterwards
var INS : Poniter;
........
INS.about := q;
And the procedure goes as follows:
{temp := L} {I'll use this for my attempt below }
if L<>NIL then
begin
while L^.next<>NIL do
begin
L:= L^.next;
end;
L^.next := INS;
INS^.next := NIL;
{L:=temp;} {I'll explain this in my attempt below}
end
else
begin
L:= INS;
end;
I also have a small procedure that prints all the elements of the linked list
procedure displayElements(L : pointer);
begin
while L <> nil do
begin
writeln(L^.about);
L := L^.next;
end
end;
The problem: after i run de program it only displays the last two entries of the list.
Conjecture about the problem: I believe it only shows the last two because when i run the procedure displayElements the pointer L is already one element before NIL -because i used the second algorithm-.
Attempt to a solution: Alright, i think i need to put L back at the very first place so that when i use displayElements it get all the elements in the list. But how could I do that?, I tried what i commented above saving L in temp but it didn't work.
Any ideas?. Thanks.
Here's a very simple program which will do what you want. Maintaining a 'tail' pointer means that you don't have to traverse the list every time that you want to add a value. If this were your code, then you would be missing the 'tail:= tmp' line in Insert: without this, Display prints the first and last entries, but not those in the middle.
type
node = ^MyRec;
MyRec = record
value: integer;
next: node
end;
var
head, tail: node;
Procedure Insert (v: integer);
var
tmp: node;
begin
new (tmp);
tmp^.value:= v;
tmp^.next:= nil;
if head = nil
then head:= tmp
else tail^.next:= tmp;
tail:= tmp;
end;
Procedure Display;
var
tmp: node;
begin
tmp:= head;
while tmp <> nil do
begin
writeln (tmp^.value);
tmp:= tmp^.next
end;
end;
begin
head:= nil;
Insert (5);
Insert (10);
Display;
Insert (15);
Display;
readln
end.
[Edit]
Judging by your comments, some further explanation is required. [Professorial mode on] When I started programming some thirty years ago (OMSI Pascal on a PDP 11/70), linked lists and pointers appeared in every self-respecting program, but since the rise of Delphi in 1990, such complexities have been hidden and most programmers now never see a naked pointer.
Linked lists come in different formats: the simple and the complex. The simple types differ at the points of insertion and deletion: a stack inserts and deletes at the same end, a queue inserts at one end and deletes at the other, a list allows insertion and deletion at any place. More complex types are trees and graphs. Your question is asking about the implementation of a queue - insertion is always at the rear, deletion is at the front.
In order to implement this properly, we need two variables: 'head' points to the head of the queue and 'tail' points to the end of the queue. These variables are normal global variables; memory is allocated for them in the data segment of the program. A pointer is a simple variable whose value is the memory address of another variable. Initially, 'head' does not point to anything so its value is nil (think of this as 0).
Normally in textbooks, the construction of a queue is accompanied by little boxes showing how memory is allocated, but I don't know how to do that here and so the explanation will be a little wordy.
When the first insertion occurs, the memory manager in the run time system allocates 12 bytes from the heap and sets the value of the local variable 'tmp' to be the address of the first of those 12 bytes (this is 'new (tmp)'). Of those 12 bytes, the 'value' part is set to 5 and the 'next' part is set to nil. Then the program checks what the value of 'head' is: if it is nil, then the value (ie address of the memory block allocated above) is copied from 'tmp' to 'head'. If 'head' already points to something, then the value of 'tmp' is copied to 'tail^.next' (which previously would have been nil). Then the value of 'tmp' is copied to the tail variable. Thus 'head' always points to the beginning of the queue and does not change whereas 'tail' points to the end of the queue and changes every time a new node is inserted.
Let's do some debugging:
When the program starts, head = nil, tail is undefined
After 'insert (5)', head = $870874, tail = $870874
After 'insert (10)', head = $870874, tail = $870880
After 'insert (15)', head = $870874, tail = $87088C
When displaying,
tmp:= head ......... tmp = $870874 (ie head)
tmp:= tmp^.next .... tmp = $870880
tmp:= tmp^.next .... tmp = $87088C
tmp:= tmp^.next .... tmp = nil
If your program has more than one queue, then you will need two variables for each queue, and 'Insert' will have to be changed in order to accept two parameters (which will be the head and tail of the given queue).
There is no need to write 'new (head)' and 'new (tail)' - doing so will cause the original pointers to the beginning and end of the queue to be lost.
[Professorial mode off] I hope that this explanation helps.

Iterating over nested lists with a Next() function, without a generator

Whilst I'd love to solve this problem in python, I'm stuck in Delphi for this one. I have nested lists (actually objects with nested lists as properties, but nevermind), and I want to iterate over them in a generator fashion. That is, I want to write a Next function, which gives me the next item from the leaves of the tree described by the nested lists.
For example, lets say I have
[[1,2,3],[4,5],[],[6],[7,8]]
I want 8 consecutive calls to Next() to return 1..8.
How can I do this in a language without yield and generators?
Note that the depth of the nesting is fixed (2 in this example, 4 in real life), but answers which solve the more general case where depth is variable are welcome.
EDIT: Sorry, I should have mentioned, this is Delphi 2007.
If you're using any Delphi list that has a built-in enumerator, it can be done easily enough with a bit of recursion. Your base list is a list of numbers, like a TList<integer>. Then you have nested lists implemented as TList<TList<integer>>. (I'm assuming you have Delphi 2009 or 2010. If not, it gets a bit trickier.)
What you need is to make your own specialized list class descended from TList<T> and add a virtual Next() function to it, and a field for an enumerator for your list. The compiler uses enumerators internally when you set up a for..in loop, but you can run them manually. The Next() function creates the enumerator if it's not already assigned and puts it in the field, then calls MoveNext() on it. If this succeeds, call GetCurrent and get your number. Otherwise, you're done. FreeAndNil the enumerator, and signal to the calling function that you've got nothing to return. Probably the simplest way to do this is to put a var boolean parameter in Next() that returns the result of its call to MoveNext, and have the calling function check its value.
For the higher lists, it's a little more complicated, but not much. Descend from the generic class you just set up, and override Next(). This one will get an enumerator that enumerates over the lists that it holds, and return the value of FEnumerator.GetCurrent.Next() until that sub-list is exhausted, then call MoveNext on its enumerator.
This should work for any depth of nested lists. Just don't try to make a list that contains both numbers and lists.
Method 1 : a "layered list" is a tree, use (or write) a tree/treenode class, and do a simple depth-first search of its values.
Method 2 : explicitly write the depth-first search for your layered list type :
TEnumerator = RECORD
private
FStack : Stack of TNestedList; //a stack, holding which level of the tree you are exploring
FIndexStack : Stack of Integer; //a twin stack, holding the index of the child you are exploring at each layer
public
Init( AList : TNestedList );
Next( var AVal : Integer ) : boolean;
end;
procedure TEnumerator.Init( AList );
begin
SetLength(FStack, 1);
FStack[0] := AList;
SetLength( FIndexStack, 1 );
FIndexStack[0] := 0;
_GoToNextValue();
end;
procedure TEnumerator._GoToNextValue();
begin
while FStack.notEmpty()
and FIndexStack.last > FStack.last.length do
begin
//we have finished exploring FStack.last, we need to explore its next sibling :
//pop FStack.last :
FIndexStack.pop;
FStack.pop;
//go to next sibling :
FIndexStack.last := FIndexStack.last + 1;
end;
//nothing left to explore :
if FStack.empty then Exit;
//else :
// dig through the layers of list until you reach a value
while FStack.last.isAList() do
begin
FStack.push( FStack.last[ FIndexStack.last ] );
FIndexStack.push( 0 );
end;
end;
function Next( var AVal : Integer ) : boolean;
begin
_GoToNextValue();
Result := FStack.notEmpty();
if Result then
begin
AVal := FStack.last[ FIndexStack.last ];
FIndexSatck.last := FIndexSatck.last + 1;
end;
end;
You basically do explicitly what a "yield" would do (ie : save a "freeze copy" of the call stack, return the current value)
Usage :
LEnum : TEnumerator;
LEnum.Init( myList );
while LEnum.Next( LVal ) do
begin
<do something>
end;
To solve this problem, I ended up making a flat list of indexes and remembering my position in that: (in pythonesque, because its shorter)
class Nested:
nestedList = [[1,2,3],[4,5],[],[6],[7,8]]
positions = []
curr = 0
def Setup:
for a in nestedList:
for b in a:
positions.add((a,b))
def Next:
p = positions[curr]
curr += 1
return nestedList[p[0]][p[1]]
Obviously my list doesn't change during iteration or this probably wouldn't work...

Best way to sort an array

Say I have an array of records which I want to sort based on one of the fields in the record. What's the best way to achieve this?
TExample = record
SortOrder : integer;
SomethingElse : string;
end;
var SomeVar : array of TExample;
You can add pointers to the elements of the array to a TList, then call TList.Sort with a comparison function, and finally create a new array and copy the values out of the TList in the desired order.
However, if you're using the next version, D2009, there is a new collections library which can sort arrays. It takes an optional IComparer<TExample> implementation for custom sorting orders. Here it is in action for your specific case:
TArray.Sort<TExample>(SomeVar , TDelegatedComparer<TExample>.Construct(
function(const Left, Right: TExample): Integer
begin
Result := TComparer<Integer>.Default.Compare(Left.SortOrder, Right.SortOrder);
end));
(I know this is a year later, but still useful stuff.)
Skamradt's suggestion to pad integer values assumes you are going to sort using a string compare. This would be slow. Calling format() for each insert, slower still. Instead, you want to do an integer compare.
You start with a record type:
TExample = record
SortOrder : integer;
SomethingElse : string;
end;
You didn't state how the records were stored, or how you wanted to access them once sorted. So let's assume you put them in a Dynamic Array:
var MyDA: Array of TExample;
...
SetLength(MyDA,NewSize); //allocate memory for the dynamic array
for i:=0 to NewSize-1 do begin //fill the array with records
MyDA[i].SortOrder := SomeInteger;
MyDA[i].SomethingElse := SomeString;
end;
Now you want to sort this array by the integer value SortOrder. If what you want out is a TStringList (so you can use the ts.Find method) then you should add each string to the list and add the SortOrder as a pointer. Then sort on the pointer:
var tsExamples: TStringList; //declare it somewhere (global or local)
...
tsExamples := tStringList.create; //allocate it somewhere (and free it later!)
...
tsExamples.Clear; //now let's use it
tsExamples.sorted := False; //don't want to sort after every add
tsExamples.Capacity := High(MyDA)+1; //don't want to increase size with every add
//an empty dynamic array has High() = -1
for i:=0 to High(MyDA) do begin
tsExamples.AddObject(MyDA[i].SomethingElse,TObject(MyDA[i].SortOrder));
end;
Note the trick of casting the Integer SortOrder into a TObject pointer, which is stored in the TStringList.Object property. (This depends upon the fact that Integer and Pointer are the same size.) Somewhere we must define a function to compare the TObject pointers:
function CompareObjects(ts:tStringList; Item1,Item2: integer): Integer;
begin
Result := CompareValue(Integer(ts.Objects[Item1]), Integer(ts.Objects[Item2]))
end;
Now, we can sort the tsList on .Object by calling .CustomSort instead of .Sort (which would sort on the string value.)
tsExamples.CustomSort(#CompareObjects); //Sort the list
The TStringList is now sorted, so you can iterate over it from 0 to .Count-1 and read the strings in sorted order.
But suppose you didn't want a TStringList, just an array in sorted order. Or the records contain more data than just the one string in this example, and your sort order is more complex. You can skip the step of adding every string, and just add the array index as Items in a TList. Do everything above the same way, except use a TList instead of TStringList:
var Mlist: TList; //a list of Pointers
...
for i:=0 to High(MyDA) do
Mlist.add(Pointer(i)); //cast the array index as a Pointer
Mlist.Sort(#CompareRecords); //using the compare function below
function CompareRecords(Item1, Item2: Integer): Integer;
var i,j: integer;
begin
i := integer(item1); //recover the index into MyDA
j := integer(item2); // and use it to access any field
Result := SomeFunctionOf(MyDA[i].SomeField) - SomeFunctionOf(MyDA[j].SomeField);
end;
Now that Mlist is sorted, use it as a lookup table to access the array in sorted order:
for i:=0 to Mlist.Count-1 do begin
Something := MyDA[integer(Mlist[i])].SomeField;
end;
As i iterates over the TList, we get back the array indexes in sorted order. We just need to cast them back to integers, since the TList thinks they're pointers.
I like doing it this way, but you could also put real pointers to array elements in the TList by adding the Address of the array element instead of it's index. Then to use them you would cast them as pointers to TExample records. This is what Barry Kelly and CoolMagic said to do in their answers.
If your need sorted by string then use sorted TStringList and
add record by TString.AddObject(string, Pointer(int_val)).
But If need sort by integer field and string - use TObjectList and after adding all records call TObjectList.Sort with necessary sorted functions as parameter.
This all depends on the number of records you are sorting. If you are only sorting less than a few hundred then the other sort methods work fine, if you are going to be sorting more, then take a good look at the old trusty Turbo Power SysTools project. There is a very good sort algorithm included in the source. One that does a very good job sorting millions of records in a efficient manner.
If you are going to use the tStringList method of sorting a list of records, make sure that your integer is padded to the right before inserting it into the list. You can use the format('%.10d',[rec.sortorder]) to right align to 10 digits for example.
The quicksort algorithm is often used when fast sorting is required. Delphi is (Or was) using it for List.Sort for example.
Delphi List can be used to sort anything, but it is an heavyweight container, which is supposed to look like an array of pointers on structures. It is heavyweight even if we use tricks like Guy Gordon in this thread (Putting index or anything in place of pointers, or putting directly values if they are smaller than 32 bits): we need to construct a list and so on...
Consequently, an alternative to easily and fastly sort an array of struct might be to use qsort C runtime function from msvcrt.dll.
Here is a declaration that might be good (Warning: code portable on windows only).
type TComparatorFunction = function(lpItem1: Pointer; lpItem2: Pointer): Integer; cdecl;
procedure qsort(base: Pointer; num: Cardinal; size: Cardinal; lpComparatorFunction: TComparatorFunction) cdecl; external 'msvcrt.dll';
Full example here.
Notice that directly sorting the array of records can be slow if the records are big. In that case, sorting an array of pointer to the records can be faster (Somehow like List approach).
With an array, I'd use either quicksort or possibly heapsort, and just change the comparison to use TExample.SortOrder, the swap part is still going to just act on the array and swap pointers. If the array is very large then you may want a linked list structure if there's a lot of insertion and deletion.
C based routines, there are several here
http://www.yendor.com/programming/sort/
Another site, but has pascal source
http://www.dcc.uchile.cl/~rbaeza/handbook/sort_a.html
Use one of the sort alorithms propose by Wikipedia. The Swap function should swap array elements using a temporary variable of the same type as the array elements. Use a stable sort if you want entries with the same SortOrder integer value to stay in the order they were in the first place.
TStringList have efficient Sort Method.
If you want Sort use a TStringList object with Sorted property to True.
NOTE: For more speed, add objects in a not Sorted TStringList and at the end change the property to True.
NOTE: For sort by integer Field, convert to String.
NOTE: If there are duplicate values, this method not is Valid.
Regards.
If you have Delphi XE2 or newer, you can try:
var
someVar: array of TExample;
list: TList<TExample>;
sortedVar: array of TExample;
begin
list := TList<TExample>.Create(someVar);
try
list.Sort;
sortedVar := list.ToArray;
finally
list.Free;
end;
end;
I created a very simple example that works correctly if the sort field is a string.
Type
THuman = Class
Public
Name: String;
Age: Byte;
Constructor Create(Name: String; Age: Integer);
End;
Constructor THuman.Create(Name: String; Age: Integer);
Begin
Self.Name:= Name;
Self.Age:= Age;
End;
Procedure Test();
Var
Human: THuman;
Humans: Array Of THuman;
List: TStringList;
Begin
SetLength(Humans, 3);
Humans[0]:= THuman.Create('David', 41);
Humans[1]:= THuman.Create('Brian', 50);
Humans[2]:= THuman.Create('Alex', 20);
List:= TStringList.Create;
List.AddObject(Humans[0].Name, TObject(Humans[0]));
List.AddObject(Humans[1].Name, TObject(Humans[1]));
List.AddObject(Humans[2].Name, TObject(Humans[2]));
List.Sort;
Human:= THuman(List.Objects[0]);
Showmessage('The first person on the list is the human ' + Human.name + '!');
List.Free;
End;

Resources