Home » SQL & PL/SQL » SQL & PL/SQL » Count number of elements in an array. (PLSQL)
Count number of elements in an array. [message #579548] Wed, 13 March 2013 02:24 Go to next message
vicktorz
Messages: 26
Registered: March 2013
Junior Member
Hi Folks,
Please help me in this..
I need to count number of elements in the same catagory of an array..
For example, an array consists of {'a','b','c','c','a','d','c'} means,
i need to display like a=2, b=2, c=3, d=1.
I have written the below code.

declare
type array_val is varray(10) of varchar2(15000);
counter number:=0;
SMQ_NAME ARRAY_VAL:=ARRAY_VAL();
begin
/* now array_val consists of these values {'a','b','c','c','a','d','c'}*/
/* now i'm trying to count no of elements repeated in an arry */

for I in SMQ_NAME.first .. SMQ_NAME.last
LOOP
a:=SMQ_NAME(I);
for J in (I+1) .. SMQ_NAME.last
LOOP
b:=SMQ_NAME(J);

if(A=B) then
COUNTER:=COUNTER+1;
end if;
end LOOP;
DBMS_OUTPUT.PUT_LINE('the value'||A||' is repeated for'||COUNTER||'times');
counter:=0;
end loop;

end;

But its not showing exact output as my requirement.. Can anyone please help in this..
Thanks in advance.Smile
-vijay
Re: Count number of elements in an array. [message #579551 is a reply to message #579548] Wed, 13 March 2013 02:43 Go to previous messageGo to next message
Michel Cadot
Messages: 68770
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Welcome to the forum.

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code, use code tags and align the columns in result.
Use the "Preview Message" or "Preview Quick Reply" button to verify.
Also always post your Oracle version, with 4 decimals.

Use SQL*Plus and copy and paste your session, the WHOLE session.

Not only your code does not show the result but it does even not compile:
SQL> declare
  2  type array_val is varray(10) of varchar2(15000);
  3  counter number:=0;
  4  SMQ_NAME ARRAY_VAL:=ARRAY_VAL();
  5  begin
  6  /* now array_val consists of these values {'a','b','c','c','a','d','c'}*/
  7  /* now i'm trying to count no of elements repeated in an arry */
  8  
  9  for I in SMQ_NAME.first .. SMQ_NAME.last
 10  LOOP
 11  a:=SMQ_NAME(I);
 12  for J in (I+1) .. SMQ_NAME.last
 13  LOOP
 14  b:=SMQ_NAME(J);
 15  
 16  if(A=B) then
 17  COUNTER:=COUNTER+1;
 18  end if;
 19  end LOOP;
 20  DBMS_OUTPUT.PUT_LINE('the value'||A||' is repeated for'||COUNTER||'times');
 21  counter:=0;
 22  end loop;
 23  
 24  end;
 25  /
a:=SMQ_NAME(I);
*
ERROR at line 11:
ORA-06550: line 11, column 1:
PLS-00363: expression 'A' cannot be used as an assignment target
ORA-06550: line 11, column 1:
PL/SQL: Statement ignored
ORA-06550: line 14, column 1:
PLS-00363: expression 'B' cannot be used as an assignment target
ORA-06550: line 14, column 1:
PL/SQL: Statement ignored
ORA-06550: line 16, column 4:
PLS-00357: Table,View Or Sequence reference 'A' not allowed in this context
ORA-06550: line 16, column 1:
PL/SQL: Statement ignored
ORA-06550: line 20, column 35:
PLS-00357: Table,View Or Sequence reference 'A' not allowed in this context
ORA-06550: line 20, column 1:
PL/SQL: Statement ignored

Regards
Michel
Re: Count number of elements in an array. [message #579552 is a reply to message #579551] Wed, 13 March 2013 02:49 Go to previous messageGo to next message
Michel Cadot
Messages: 68770
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator
Extending your exercise, you can do it with a permanent type and Oracle SQL features:
SQL> create or replace type array_val is varray(10) of varchar2(15);
  2  /

Type created.

SQL> declare
  2  --  type array_val is varray(10) of varchar2(15);
  3    SMQ_NAME ARRAY_VAL := ARRAY_VAL('a','b','c','c','a','d','c'); 
  4  begin
  5    for rec in (select column_value, count(*) nb from table(smq_name) 
  6                group by column_value order by column_value) 
  7    loop
  8      dbms_output.put_line('The value '||rec.column_value||' is repeated for '||rec.nb||' times');
  9    end loop;
 10  end;
 11  /
The value a is repeated for 2 times
The value b is repeated for 1 times
The value c is repeated for 3 times
The value d is repeated for 1 times

PL/SQL procedure successfully completed.

Regards
Michel
Re: Count number of elements in an array. [message #579554 is a reply to message #579548] Wed, 13 March 2013 02:51 Go to previous messageGo to next message
dariyoosh
Messages: 538
Registered: March 2009
Location: France
Senior Member
Hi


You don't need to implement your own counter. There is already a function for that in SQL-PL/SQL
Please read COUNT

You will also need to use COLUMN_VALUE pseudocolumn with TABLE in order to do a SELECT COUNT based on the returned value.


Regards,
Dariyoosh
Re: Count number of elements in an array. [message #579575 is a reply to message #579554] Wed, 13 March 2013 06:59 Go to previous messageGo to next message
vicktorz
Messages: 26
Registered: March 2013
Junior Member
got expected output.. Thanks Michel and dariyoosh:)
Re: Count number of elements in an array. [message #579587 is a reply to message #579548] Wed, 13 March 2013 08:36 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3305
Registered: January 2010
Location: Connecticut, USA
Senior Member
Declaring SQL type, as Michel already showed, makes it much easier. Just for fun, using PL/SQL type:

set serveroutput on
declare
    type array_val is varray(10) of varchar2(15000);
    smq_name array_val := array_val('a','b','c','c','a','d','c'); 
    type val_array is table of number
      index by varchar2(15000);
    v_arr val_array;
    v_idx varchar2(15000);
begin
    for i in 1..smq_name.count loop
      if v_arr.exists(smq_name(i))
        then
          v_arr(smq_name(i)) := v_arr(smq_name(i)) + 1;
        else
          v_arr(smq_name(i)) := 1;
      end if;
    end loop;
    v_idx := v_arr.first;
    loop
      dbms_output.put_line('Value ' || v_idx || ' is repeated '|| v_arr(v_idx) || ' times');
      v_idx := v_arr.next(v_idx);
      exit when v_idx is null;
    end loop;
end;
/
Value a is repeated 2 times
Value b is repeated 1 times
Value c is repeated 3 times
Value d is repeated 1 times

PL/SQL procedure successfully completed.

SQL> 


SY.
Re: Count number of elements in an array. [message #579702 is a reply to message #579587] Thu, 14 March 2013 02:30 Go to previous message
vicktorz
Messages: 26
Registered: March 2013
Junior Member
Hey thanks man.. Really its fun.. Thanks for the code n logic...Smile

-vijay..
Previous Topic: Creating a New Session
Next Topic: How to find whether the Exact string is present or not in a given string
Goto Forum:
  


Current Time: Wed Sep 03 06:52:16 CDT 2025