I declare an associate array like this
TYPE t_a_rec IS RECORD
(
a_id NUMBER,
a_name VARCHAR2 (500),
assigned NUMBER:= 0
);
TYPE t_a IS TABLE OF t_a_rec
INDEX BY PLS_INTEGER;
a t_a;
and in a part of code i need to reset all the assigned values to zero. so my code is:
IF flag = TRUE
THEN
FOR i IN a.FIRST .. a.LAST
LOOP
a(i).assigned := 0;
END LOOP;
END IF;
Is there any way to reset without loop?
In example something like:
IF flag = TRUE
THEN
a(:).assigned := 0; or a(all).assigned := 0;
END IF;
In case of associative arrays You don't have any option other than looping throgh it to reset one value from Your arrays element. Please read through the processing associative arrays in loops. You are looping the arryas wrong. Please consider the commented code in this example.
declare
type t_a_rec is record(
a_id number,
a_name varchar2(500),
assigned number := 0);
type t_a is table of t_a_rec index by pls_integer;
a t_a;
li_idx int;
begin
a(1).a_id := 1;
a(1).a_name := 'Test Name';
a(1).assigned := 100;
a(3).a_id := 2;
a(3).a_name := 'Test Name 2';
a(3).assigned := 200;
/* for i in a.first .. a.last loop
dbms_output.put_line(a(i).assigned);
end loop;*/
li_idx := a.first;
while (li_idx is not null) loop
dbms_output.put_line(a(li_idx).assigned);
li_idx := a.next(li_idx);
end loop;
end;
If You remove the comment You will get error, because You don't have values in Your arrays with 2 index. The for loop will iterate from the first value of it's attribute(a.first - which is 1 in the example above) and increment it up to the second value of it's attibute(a.last whichi is the 3). So You will have 1,2,3 for the looping indexes and in the second step You will get no data found error for the array.
Related
I need to remove all duplicate values from an array of integer, yet maintain the order of the elements:
Example:
10,20,20(duplicate),10(duplicate),50
Becomes:
10,20,50
Create a dictionary with Integer as the key. The value type is immaterial.
Iterate through the input array. For each value in the input array, check whether or not that value is in the dictionary.
If yes, this is a duplicate, discard.
If no, this is the first time the value has been encountered. Retain the value, and add it to the dictionary.
The point of the dictionary is that it can perform O(1) lookup.
In pseudocode:
var
arr: TArray<Integer>; // input and output
Dict: TDictionary<Integer, Integer>;
SrcIndex, DestIndex: Integer;
....
DestIndex := 0;
for SrcIndex := 0 to high(arr) do begin
Value := arr[SrcIndex];
if not Dict.ContainsKey(Value) then begin
arr[DestIndex] := arr[SrcIndex];
Dict.Add(Value, 0);
inc(DestIndex);
end;
end;
SetLength(arr, DestIndex);
Obviously you need to create, and destroy, the dictionary. I'm assuming you know how to do that. And I've opted to modify the array in place but you could equally create a new array if you prefer.
heres a version without dictionary.
procedure TForm1.RemoveDuplicates;
var
i,j,k,tot,mov:integer;
arr:array of integer;
begin
arr := [10,20,30,40,30,20,10,10,50,10,20,40];
tot := 0;
for i := 0 to length(arr)-1 do
begin
if i >= length(arr)-tot-1 then
break;
for j := i + 1 to length(arr)-1-tot do
begin
if j >= length(arr)-tot-1 then
break;
mov := 0;
while arr[i] = arr[j] do
begin
inc(mov);
arr[j] := arr[j+mov];
end;
tot := tot + mov;
if mov>0 then
for k := j+1 to length(arr)-1-tot do
arr[k] := arr[k+mov];
end;
end;
SetLength(arr,length(arr)-tot-1);
end;
I have multiple arrays and they all start with integer fields, from 1 up to 5 fields, and these are like indexes that need to be sorted, from min to max:
TArrayA = record
Field1:integer;
Field2:integer;
Field3:integer;
Field4:integer;
Field5:integer;
... //other fields, strings, integers... up to 50 fields
end;
ArrayA:=array of TArrrayA;
Currently I use this approach to sort:
// sort by Field1
top:=Length(ArrayA);
for counter := 0 to top do
begin
min := counter;
for look := counter + 1 to top do
if ArrayA[look].Field1 < ArrayA[min].Field1 then
min := look;
vTmpRecord := ArrayA[min];
ArrayA[min] := ArrayA[counter];
ArrayA[counter] := vTmpRecord;
end;
// now sort by Field2
top:=Length(ArrayA);
for counter := 0 to top do
begin
min := counter;
for look := counter + 1 to top do
if (ArrayA[look].Field1 = ArrayA[min].Field1) And
(ArrayA[look].Field2 < ArrayA[min].Field2) then
min := look;
vTmpRecord := ArrayA[min];
ArrayA[min] := ArrayA[counter];
ArrayA[counter] := vTmpRecord;
end;
This does the job. Although is a bit slow when I need to sort all 5 fields,
and this is how I do it, field by field, so I sort the array 5 times. Is there any better, faster way?
Here is example:
procedure TForm1.Button8Click(Sender: TObject);
type
TArrayA = record
Field1: integer;
Field2: integer;
Field3: integer;
Field4: integer;
Field5: integer;
end;
var
ArrayA: array of TArrayA;
vTmpRecord: TArrayA;
top, counter, min, max, look: integer;
i,t1,t2:integer;
begin
SetLength(ArrayA,100000);
for i := 0 to 99999 do
begin
ArrayA[i].Field1:=1+Random(100);
ArrayA[i].Field2:=1+Random(100);
ArrayA[i].Field3:=1+Random(100);
ArrayA[i].Field4:=1+Random(100);
ArrayA[i].Field5:=1+Random(100);
end;
t1:=GetTickCount;
// sort by Field1
top := Length(ArrayA);
for counter := 0 to top do
begin
min := counter;
for look := counter + 1 to top do
if ArrayA[look].Field1 < ArrayA[min].Field1 then
min := look;
vTmpRecord := ArrayA[min];
ArrayA[min] := ArrayA[counter];
ArrayA[counter] := vTmpRecord;
end;
// sort by Field2
top := Length(ArrayA);
for counter := 0 to top do
begin
min := counter;
for look := counter + 1 to top do
if (ArrayA[look].Field1 = ArrayA[min].Field1) and
(ArrayA[look].Field2 < ArrayA[min].Field2) then
min := look;
vTmpRecord := ArrayA[min];
ArrayA[min] := ArrayA[counter];
ArrayA[counter] := vTmpRecord;
end;
// sort by Field3
top := Length(ArrayA);
for counter := 0 to top do
begin
min := counter;
for look := counter + 1 to top do
if (ArrayA[look].Field1 = ArrayA[min].Field1) and (ArrayA[look].Field2 = ArrayA[min].Field2) and
(ArrayA[look].Field3 < ArrayA[min].Field3) then
min := look;
vTmpRecord := ArrayA[min];
ArrayA[min] := ArrayA[counter];
ArrayA[counter] := vTmpRecord;
end;
// sort by Field4
top := Length(ArrayA);
for counter := 0 to top do
begin
min := counter;
for look := counter + 1 to top do
if (ArrayA[look].Field1 = ArrayA[min].Field1) and (ArrayA[look].Field2 = ArrayA[min].Field2) and (ArrayA[look].Field3 = ArrayA[min].Field3) and
(ArrayA[look].Field4 < ArrayA[min].Field4) then
min := look;
vTmpRecord := ArrayA[min];
ArrayA[min] := ArrayA[counter];
ArrayA[counter] := vTmpRecord;
end;
// sort by Field5
top := Length(ArrayA);
for counter := 0 to top do
begin
min := counter;
for look := counter + 1 to top do
if (ArrayA[look].Field1 = ArrayA[min].Field1) and (ArrayA[look].Field2 = ArrayA[min].Field2) and (ArrayA[look].Field3 = ArrayA[min].Field3) and (ArrayA[look].Field4 = ArrayA[min].Field4) and
(ArrayA[look].Field5 < ArrayA[min].Field5) then
min := look;
vTmpRecord := ArrayA[min];
ArrayA[min] := ArrayA[counter];
ArrayA[counter] := vTmpRecord;
end;
t2:=GetTickCount;
Button8.Caption:=IntToStr(t2-t1);
end;
You can use built in Quick sort method for sorting arrays with your custom comparer:
uses
System.Math,
System.Generics.Defaults,
System.Generics.Collections;
TArray.Sort<TArrayA>(ArrayA, TComparer<TArrayA>.Construct( function(const Left, Right: TArrayA): Integer
begin
if Left.Field1 = Right.Field1 then
begin
if Left.Field2 = Right.Field2 then
begin
Result := CompareValue(Left.Field3, Right.Field3);
end
else Result := CompareValue(Left.Field2, Right.Field2);
end
else Result := CompareValue(Left.Field1, Right.Field1);
end
));
I added code only for first three fields, but you will get the picture how to build your own comparer for more fields.
The most important thing for you to do is to separate the sort algorithm from the data. That way you can write, or use, a single sort algorithm again and again with different data
The classic way to do that is to use a comparison sort. They are sort algorithms that require a compare function that compares two items and returns a negative integer for less than, a positive integer for greater than, and zero when equal.
So, let's start by demonstrating such a compare function for your data. Storing multiple fields as you have makes it hard to write a general purpose comparer. Better to put the fields in an array. Once you have done so you can do the compare lexicographically using iteration like this:
function CompareIntegerArray(const lhs, rhs: array of Integer): Integer;
var
i: Integer;
begin
Assert(Length(lhs) = Length(rhs));
for i := low(lhs) to high(lhs) do
if lhs[i] < rhs[i] then
exit(-1)
else if lhs[i] > rhs[i] then
exit(1);
exit(0);
end;
With a lexicographic order we first compare the primary field. If they differ we have our answer, otherwise we move on to the secondary field. And so on. Such an algorithm is well suited to iteration as demonstrated above.
This overcomes a significant weakness in your approach, by sorting the array once only.
Once you have this compare function you need to wrap it in an outer compare function that extracts data from the record fields and populates arrays. Perhaps along these lines:
type
TMyArray = array [1..5] of Integer;
function GetMyArray(const Value: TArrayA): TMyArray;
begin
Result[1] := Value.Field1;
Result[2] := Value.Field2;
....
end;
function MyCompare(const lhs, rhs: TArrayA): Integer;
begin
Result := CompareIntegerArray(
GetMyArray(lhs),
GetMyArray(rhs)
);
end;
Now, as promised, you can use this compare function with a general purpose sort like TArray.Sort<T> from Generics.Collections. This is an implementation of Quicksort, a comparison sort with average complexity of O(n log n). That will typically yield a huge benefit over your O(n2) bubble sort.
Life would be simpler if you could replace the record with an actual array. Another option that might be useful would be to add a method to the record that returned an array of integer ready for use in the lexicographic compare function.
To recap:
Separate data, comparison and sorting to facilitate re-use and clarity.
Use arrays to enable lexicographic compare to be implemented with a loop.
Use an efficient sort algorithm such as Quicksort.
Is there any way to have sum of values of associate array in PL/SQL. normally the code is like this:
FOR i IN a.FIRST .. a.LAST
LOOP
IF a (i).weight > 0
THEN
flag := FALSE;
END IF;
END LOOP;
but it should be a way to do it without loop and using sum.
something like:
IF SUM(a.weight) > 0
THEN
flag := FALSE;
END IF;
Actually - normally the code above is wrong for looping through associative arrays. Try this.
declare
type t_array_rec is record(
weight number);
type t_array is table of t_array_rec index by pls_integer;
arr t_array;
li_idx int;
li_summ int := 0;
begin
arr(1).weight := 100;
arr(3).weight:= 200;
arr(5).weight := 150;
li_idx := arr.first;
while (li_idx is not null) loop
li_summ := li_summ + nvl(arr(li_idx).weight, 0);
li_idx := arr.next(li_idx);
end loop;
dbms_output.put_line(li_summ);
end;
This will count the min value. Also You can kindly have a look at the another answer on SO sorting the assotiative arrays .
In case of nested tables, You can use the table functions.
Assuming You have the TTI type on the database layer.
CREATE OR REPLACE TYPE TTI as table of int
You can aggragate it's values like shown bellow
declare
arr TTI := TTI(100, 200, 150);
li_summ number;
begin
select sum(column_value) into li_summ from table(arr);
dbms_output.put_line(li_summ);
end;
Please what would be the fastest (and most 'elegant') way to search for FaDirection array in the multi dim. FdVSubs array?
TaDirection = array[0..7] of TSubRect; //TSubRect = class(TObject)
Multi dim array:
FdVSubs: array[0..15] of TaDirection;
Array:
FaDirection : TaDirection;
I need to resolve if FaDirection is already stored in FdVSubs or not.
Thank You.
First and most important, you need to decide what defines two arrays as being equal. Then you'll have to adjust my code to match that definition. For the answer I'll assume arrays are equal if and only if they have identical object references at each corresponding position in the arrays.
function DirectionsAreEqual(const ADirection1, ADirection2: TaDirection): Boolean;
var
LoopI: Integer;
begin
Result := True;
for LoopI := Low(TaDirection) to High(TaDirection) do
begin
if (ADirection1[LoopI] <> ADirection2[LoopI]) then
begin
Result := False;
Exit;
end;
end;
end;
function DirectionExistsIn(const ADirection: TaDirection;
const ADirectionList: array of TaDirection): Boolean;
var
LoopI: Integer;
begin
Result := False; //Caters for empty DirectionList
for LoopI := Low(ADirectionList) to High(ADirectionList) do
begin
Result := DirectionsAreEqual(ADirection, ADirectionList[LoopI]);
if (Result) then
begin
Exit;
end;
end;
end;
//Then you can simply do the test as follows:
if DirectionExistsIn(FaDirection, FdVSubs) then ...
NOTES
DirectionExistsIn uses an Open Array, so it doesn't matter how many elements are defined for the FdVSubs array.
A small change to DirectionExistsIn can allow you to return the position of the duplicate.
guys. Here's a simple sample two-dimensional array in PL/SQL, which is working perfectly.
declare
type a is table of number;
type b is table of a;
arr b := b(a(1, 2), a(3, 4));
begin
for i in arr.first .. arr.last loop
for j in arr(i).first .. arr(i).last loop
dbms_output.put_line(arr(i) (j));
end loop;
end loop;
end;
What I need to do, is to create something similar for a table of RECORDS. Like this:
type a is record(a1 number, a2 number);
type b is table of a;
The question is, can I manually initialize this kind of array, or it is supposed to be filled by bulk collects or similar? The same syntax as above doesn't seem to work, and I wasn't able to find any initialization sample in manuals.
There is no "constructor" syntax for RECORDs, so you have to populate them like this:
declare
type a is record(a1 number, a2 number);
type b is table of a;
arr b := b();
begin
arr.extend(2);
arr(1).a1 := 1;
arr(1).a2 := 2;
arr(2).a1 := 3;
arr(2).a2 := 4;
end;
This works without objects, but you have to declare a constructor function for type 'a' values.
declare
type a is record(a1 number, a2 number);
type b is table of a;
arr b;
--Constructor for type a
function a_(a1 number, a2 number) return a is
r_a a;
begin
r_a.a1 := a1;
r_a.a2 := a2;
return(r_a);
end;
begin
arr := b(a_(1, 2), a_(3, 4), a_(5, 6), a_(7, 8));
for i in arr.first .. arr.last loop
dbms_output.put_line(arr(i).a1||', '||arr(i).a2);
end loop;
end;
Since release 18c Qualified Expressions provides an alternative way to define the values of complex data types. Quote:
Starting with Oracle Database Release 18c, any PL/SQL value can be provided by an expression (for example for a record or for an associative array) like a constructor provides an abstract datatype value. In PL/SQL, we use the terms "qualified expression" and "aggregate" rather than the SQL term "type constructor", but the functionality is the same.
Here's an working example:
declare
type a is record (a1 number, a2 number);
type b is table of a index by varchar2 (16);
arr b := b ('key1' => a (1, 2), 'key2' => a (3, 4));
begin
declare key varchar2 (16) := arr.first; begin
<<foreach>> loop
dbms_output.put_line (arr(key).a1||','||arr (key).a2);
key := arr.next (key);
exit foreach when key is null;
end loop; end;
end;
/
PL/SQL procedure successfully completed.
1,2
3,4