Home » SQL & PL/SQL » SQL & PL/SQL » count numbers in for loop (pl sql)
count numbers in for loop [message #346516] Mon, 08 September 2008 14:49 Go to next message
wire
Messages: 5
Registered: September 2008
Junior Member
I have a question for an assignment which I'm making at the moment.

I need to check wether an euro biljet is legal or not. The biljet has 10 numbers which need to be count and this needs to be divided by 9. When it is a whole number (like 4 and not 4.232323) then the biljet is legal. I need to do this with a for loop.

example:

biljet with the numbers: 1234567890 = 1+2+3+4+5+6+7+8+9+0 = 45 / 9 = 5 ... this is a legal biljet

I've come up with the following things but they aren't working out the way it should be and I just can't see the light Wink

declare
v_biljetnumber number := &biljetnumber;
-- this will prompt for the biljetnumber

begin
-- here I should begin with the biljetnumber and do something with substr to assign the position of the number and put it in a for loop but I really don't know how to do it....... pls can someone give me a hint or help me out? thanks in advance

v_biljetnumber := &biljetnumber;
for v_biljetnumber in v_biljetnumber loop
v_biljetnumber = substr(v_biljetnumber




Re: count numbers in for loop [message #346518 is a reply to message #346516] Mon, 08 September 2008 14:52 Go to previous messageGo to next message
BlackSwan
Messages: 25033
Registered: January 2009
Location: SoCal
Senior Member
http://www.orafaq.com/forum/t/88153/0/
Please read & follow posting guidelines as stated in URL above

Should this be moved to the HOMEWORK sub-forum?
Re: count numbers in for loop [message #346522 is a reply to message #346518] Mon, 08 September 2008 15:02 Go to previous messageGo to next message
wire
Messages: 5
Registered: September 2008
Junior Member
sorry you're right, probably better be moved to the homework sub, excuse me for not posting regarding the rules.
Re: count numbers in for loop [message #346531 is a reply to message #346516] Mon, 08 September 2008 15:28 Go to previous messageGo to next message
BlackSwan
Messages: 25033
Registered: January 2009
Location: SoCal
Senior Member
Don't you want to work with 1 character at a time?
How will you know when you've gotten to the rightmost character in the string?
Re: count numbers in for loop [message #346536 is a reply to message #346531] Mon, 08 September 2008 15:49 Go to previous messageGo to next message
wire
Messages: 5
Registered: September 2008
Junior Member
I could work with one character at a time and just count the seperate characters but the thing is I need to do it in a for loop..
Re: count numbers in for loop [message #346538 is a reply to message #346516] Mon, 08 September 2008 16:01 Go to previous messageGo to next message
BlackSwan
Messages: 25033
Registered: January 2009
Location: SoCal
Senior Member
> I need to do it in a for loop..
Inside the FOR LOOP, work with only "current" character (1 at a time).

How will you determine the FOR LOOP should terminate?


CNT := 0;
FOR ..... LOOP

   CNT := CNT + 1;

END LOOP;


What will CNT equal once the LOOP completes?
Re: count numbers in for loop [message #346541 is a reply to message #346538] Mon, 08 September 2008 16:28 Go to previous messageGo to next message
wire
Messages: 5
Registered: September 2008
Junior Member
the loop should terminate when when cnt = 10..

after every loop cnt gets + 1 so when cnt = 10 the loop has to end. but when I end the loop the numbers aren't counted yet..

I could do this with substr(v_biljetnumber (m,n)) .. but I don't know how I can do this automaticly for every number.

Thanks for giving me the hint!!!
Re: count numbers in for loop [message #346544 is a reply to message #346516] Mon, 08 September 2008 16:48 Go to previous messageGo to next message
BlackSwan
Messages: 25033
Registered: January 2009
Location: SoCal
Senior Member
>substr(v_biljetnumber (m,n))
syntax not correct/valid



TOTAL := 0;
TOTAL := TOTAL + substr(v_biljetnumber,1,1);
TOTAL := TOTAL + substr(v_biljetnumber,2,1);
etc.
Re: count numbers in for loop [message #346546 is a reply to message #346544] Mon, 08 September 2008 17:02 Go to previous messageGo to next message
wire
Messages: 5
Registered: September 2008
Junior Member
thanks! I'm going to sleep now.. with your help I'm going to fix this assignment tomorrow morning Smile

thanks a lot!
Re: count numbers in for loop [message #346620 is a reply to message #346538] Tue, 09 September 2008 02:45 Go to previous messageGo to next message
Frank
Messages: 7880
Registered: March 2000
Senior Member
anacedent wrote on Mon, 08 September 2008 23:01


CNT := 0;
FOR ..... LOOP

   CNT := CNT + 1;

END LOOP;


If you use a for-loop, there is (in general) no need to keep a separate counter; the for-loop itself introduces one.
Re: count numbers in for loop [message #346864 is a reply to message #346620] Tue, 09 September 2008 23:27 Go to previous messageGo to next message
panyam
Messages: 146
Registered: May 2008
Senior Member
Hi Wire ...this will work..But in future try by yourself then only you can easliy learn the things.

declare
var1 number := &var1;
len number := length(var1);
sum1 number := 0;
ou number := 0;
ou1 number := 0;
begin
if len = 10 then
for i in 1..10
loop
sum1 := sum1 + substr(var1,i,1);
end loop;
dbms_output.put_line('SUM ' || sum1);
ou := sum1/9;
if  ( instr(ou,'.') )<> 0 then 
dbms_output.put_line('Float' || ou);
else
dbms_output.put_line('Integer' || ou);
end if;
else
dbms_output.put_line('Length should be 10');
end if;
end;
/


SQL> /
Enter value for var1: 1234567890
old   2: var1 number := &var1;
new   2: var1 number := 1234567890;
SUM 45
Integer5

PL/SQL procedure successfully completed.

SQL> /
Enter value for var1: 1234567880
old   2: var1 number := &var1;
new   2: var1 number := 1234567880;
SUM 44
Float4.88888888888888888888888888888888888889

PL/SQL procedure successfully completed.[CODE][/CODE]

[Updated on: Tue, 09 September 2008 23:39] by Moderator

Report message to a moderator

Re: count numbers in for loop [message #346870 is a reply to message #346864] Tue, 09 September 2008 23:40 Go to previous messageGo to next message
Michel Cadot
Messages: 64103
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Quote:
But in future try by yourself then only you can easliy learn the things.

Why in the future and not NOW?

Regards
Michel
Re: count numbers in for loop [message #347494 is a reply to message #346870] Thu, 11 September 2008 20:49 Go to previous messageGo to next message
oracledinesh22
Messages: 3
Registered: July 2008
Junior Member
This is very necessary for learning process
Re: count numbers in for loop [message #377213 is a reply to message #346516] Sun, 21 December 2008 19:22 Go to previous message
wmgonzalbo
Messages: 98
Registered: November 2008
Member
You can use modulus division for this. Search for it and you'll learn something new.

Regards,
Wilbert
Previous Topic: running string concatenation
Next Topic: Dual Column Advance Constraint.
Goto Forum:
  


Current Time: Sat Dec 03 08:11:03 CST 2016

Total time taken to generate the page: 0.08370 seconds