Error: duplicate case label free pascal compiler - freepascal

I have the error "Error: duplicate case label", this is because i am using free pascal compiler, I have looked everywhere yet cannot find a solution, could you please provide me with one, thank you.
I will upload the full code in case there is something I am missing.
Sorry that it is messy.
program diceroll;
uses crt;
var count,time,double,dice1,dice2:integer;
sum1,sum2,sum3,sum4,sum5,sum6:integer;
idk:boolean;
Function Is_Double(d1,d2:integer):boolean;
begin
if d1 = d2 then
Is_Double:=true
else
Is_Double:=false;
end;
begin
randomize;
clrscr;
writeln('How many times do you want to roll the dice');
writeln(' ');
readln(time);
double:=0;
sum1:=0;
sum2:=0;
sum3:=0;
sum4:=0;
sum5:=0;
sum6:=0;
repeat
begin
dice1:=random(6)+1;
dice2:=random(6)+1;
idk:=Is_Double(dice1,dice2);
count:= count + 1;
if (idk = true) then
begin
double:= double + 1;
writeln(dice1,' ',dice2,' ','true');
end
else
writeln(dice1,' ',dice2,' ','true');
end;
if idk=true then
begin
case dice1 of
1:sum1:=sum1+1;
1:sum2:=sum2+1;
1:sum3:=sum3+1;
1:sum4:=sum4+1;
1:sum5:=sum5+1;
1:sum6:=sum6+1;
end;
until count = time;
writeln(double);
writeln(' ');
writeln(' ');
writeln(' ');
writeln(' ');
writeln(' Amount of doubles ');
writeln('1 2 3 4 5 6');
writeln(sum1,' ',sum2,' ',sum3,' ',sum4,' ',sum5,' ',sum6);
readln;
end.
Thank you

It's right here:
case dice1 of
1:sum1:=sum1+1;
1:sum2:=sum2+1;
1:sum3:=sum3+1;
1:sum4:=sum4+1;
1:sum5:=sum5+1;
1:sum6:=sum6+1;
It should be something like:
case dice1 of
1:sum1:=sum1+1;
2:sum2:=sum2+1;
3:sum3:=sum3+1;
4:sum4:=sum4+1;
5:sum5:=sum5+1;
6:sum6:=sum6+1;
Also your BEGIN... END structures look fishy to me:
repeat
// REPEAT doesn't need BEGIN
dice1:=random(6)+1;
dice2:=random(6)+1;
idk:=Is_Double(dice1,dice2);
count:= count + 1;
if (idk = true) then
begin
double:= double + 1;
writeln(dice1,' ',dice2,' ','true');
end
else
writeln(dice1,' ',dice2,' ','true');
// one extra END; removied - the one closing the unnecessory BEGIN at the start of REPEAT
if idk=true then
begin
case dice1 of
1:sum1:=sum1+1;
2:sum2:=sum2+1;
3:sum3:=sum3+1;
4:sum4:=sum4+1;
5:sum5:=sum5+1;
6:sum6:=sum6+1;
end; // CASE must have an END;
end;
until count = time;

Related

PHP foreach in SQL

I need to generate a database with uniqe codes a-z, A-Z and 0-9 with 6 digits or more(62^6=56800235584 uniqe codes)
Until now i have this code for sql:
CREATE FUNCTION cod12() RETURNS VARCHAR(10)
BEGIN
DECLARE chars VARCHAR(62);
DECLARE result VARCHAR(6);
DECLARE i INT;
SET chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvxyz';
SET result = '';
SET i = 0;
label: LOOP
SET result = CONCAT(result, SUBSTRING(chars, FLOOR(RAND()*62) + 1, 1));
SET i = i + 1;
IF i = 10 THEN
LEAVE label;
END IF;
END LOOP label;
RETURN result;
when i call function is generate a random code, from my tests i can instert 10.000 codes per seconds.
PHP version, wich is perfect solution but can generate only 300 codes per seconds, and if fails will start from begining, is a simple foreach code:
$char=array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q');
foreach ($char as $c) {
foreach ($char as $c2) {
foreach ($char as $c3) {
foreach ($char as $c4) {
echo $c['c'].$c2['c'].$c3['c'].$c4['c'].'<br>';
}}}}
Question:Loop in sql don't work, it generate only one code, how i can generate codes in order way(00,01,02...0t,0u,21,22,23,2b,2c...gj,gk,gl), not random, in sql command not php.
Thanks in advance!

How to properly program a "function" in verilog, for this specific example?

I have 3 values saved on Flip-Flops. During a certain state on a FSM I want to detect which is the bigger value and as a result of this, output into a memory a number.
In a side of the top-module file, I'm writing the function like this:
function [1:0] max_val;
input [7:-24] A, B, C;
begin
if (A > B)
begin
if (A > C)
max_val = 2'b01;
else
max_val = 2'b11;
end
else if(B > C)
max_val = 2'b10;
else
max_val = 2'b11;
else
max_val = 2b'00;
end
endfunction
Then during a state of the FSM I do this:
S13:
begin
case (max_val(FF_v1, FF_v2, FF_v3)) /// HERE??
01:
begin
mem_out1 = 1;
end
10:
begin
mem_out2 = 1;
end
11:
begin
mem_out3 = 1;
end
00:
begin
... /// what to do here??
end
endcase
end
I would like to ask if I'm defining and calling the function correctly, when I make use of the function, I understand I must use the same name of the function as "variable" and use it to define a case, right? So how do I define the input of that function, just like I did in "HERE???"
Also, if there are only 3 possible answers, and the one the combinations is unused, what to define in default??
The function declaration is right. Why the input [7:-24] changed it to input [23:0] . Also the if statement nesting was incorrect.
function [1:0] max_val;
input [23:0] A, B, C;
begin
max_val = 0 ;
if (A > B)
begin
if (A > C)
max_val = 2'b01;
else
max_val = 2'b11;
end
else
if(B > C)
max_val = 2'b10;
else
max_val = 2'b11;
end
endfunction
The case statement is fine . Though the values have to be prefixed with 2'b else it will assume the value to be decimal and not match. The case statement need not always have all the options. You can have only 3 or as many as you need . It is better to add a default statement. You can assign the values to x in the default or just print error for simulation purposes.
case ( max_val(a,b,c) )
2'b01 : begin
mem_out_1 = 1;
end
2'b10 : begin
mem_out_2 = 1;
end
2'b11 : begin
mem_out_3 = 1;
end
// no need for 00 case
default : begin
$display(" error val ");
end
endcase
http://www.testbench.in/TB_18_TASK_AND_FUNCTION.html
When I am coding this type of thing I like to "definitively" state what the outputs are going to be for each case, including the default, even if it is an error.
The point being that you could get a compiler that decides to only assign mem_out1 when you hit the 2'b01 state and leaves it set forever because nothing ever sets it back to zero. Not sure if that was the point of the design or if it might be undesired behavior.
case ( max_val(a,b,c) )
2'b01 : begin
{mem_out_3,mem_out_2,mem_out_1} = 3'b001;
end
2'b10 : begin
{mem_out_3,mem_out_2,mem_out_1} = 3'b010;
end
2'b11 : begin
{mem_out_3,mem_out_2,mem_out_1} = 3'b100;
end
// no need for 00 case
default : begin
// {mem_out_3,mem_out_2,mem_out_1} = 3'b000; // Uncomment if you need to control the output in the event of an error
$display(" error val ");
end
endcase

How to write a MYSQL CASE WHEN statement with multiple search conditions?

I know languages like PHP has switch case control structure that supports multiple validations in a single case statement like,
Switch $x {
case 1,2,3:
$a = 0;
break;
case 5,6:
$a = 1;
break;
}
Similarly can this be done in MYSQL? I tried below, which really didn't work though :(
CASE vc_shape
WHEN ('02' OR '51') THEN SET dc_square_1 = dc_square_1 + dc_row_total;
WHEN ('06' OR '30' OR 83) THEN SET dc_square_2 = dc_square_2 + dc_row_total;
.....
.....
ELSE
BEGIN
END;
END CASE;
Any ideas how can I achieve this?
Use the other format for CASE statements:
CASE
WHEN vc_shape IN ('02', '51') THEN SET dc_square_1 = dc_square_1 + dc_row_total;
WHEN vc_shape IN ('06', '30', '83') THEN SET dc_square_2 = dc_square_2 + dc_row_total;
.....
.....
ELSE
BEGIN
END;
END CASE;

Postgresql: dump of input key/value pairs into a string variable in PL/pgSQL

I need to find a way to dump key/value pairs of PL/pgSQL function input parameters:
CREATE OR REPLACE FUNCTION _test(A text, B text)
...
raise info 'Params dump: %', _x;
...
when executed:
select _text('HELLO', 'WORLD');
the function raises info as follows:
'A = HELLO, B = WORLD'
Is there a way to dump input parameter key/value pairs into a variable?
It's possible if you can make the function VARIADIC with uniform argument types, and can print the array. You don't get argument names, since they don't have names, but you do get argument positions.
Otherwise no, it is not possible in PL/PgSQL, though it should be in other PLs like PL/Perl, PL/Python, etc.
It'd be quite nice to be able to get a RECORD with all the function arguments in it, so you could print it, feed it to the hstore extension, etc, but this isn't currently possible.
There is an awkward way of dumping input parameters :
create or replace function _tester(
_txt text,
_int int
) returns void
language 'plpgsql' as
$$
declare
_out text = '';
_rec record;
_func_name text = '_tester';
begin
for _rec in SELECT parameters.ordinal_position as _pos, parameters.parameter_name as _nm
FROM information_schema.routines
JOIN information_schema.parameters
ON routines.specific_name=parameters.specific_name
WHERE routines.routine_name = _func_name
ORDER BY parameters.ordinal_position
loop
if _rec._pos = 1 then
_out = _out || _rec._nm || ' = ' || $1::text || chr(10);
elsif _rec._pos = 2 then
_out = _out || _rec._nm || ' = ' || $2::text || chr(10);
end if;
end loop;
raise notice '%', _out;
end;
$$;
select _tester('A','1');
NOTICE: _txt = A
_int = 1
Notice that must add as many if/elsif as there are input parameters. Not sure if that part can be more concise.

Length Check In Pascal

I get the error Error: Operator is not overloaded on line 7. Do I have to do a another repeat and can't use the and operator?
Function GetValidPlayerName : String;
Var
PlayerName : String;
Begin
Repeat
Readln(PlayerName);
If PlayerName = '' And Length(PlayerName) > 10
Then Write('That was not a valid name. Please try again: ');
Until PlayerName <> '';
GetValidPlayerName := PlayerName;
End;
First, you need to write
If (PlayerName = '') And (Length(PlayerName) > 10) Then
The parentheses are required.
Secondly, this will always evaluate to false, because there is no string that is both empty and has length 11 or more. Indeed, a string is empty if and only if its length is zero, so basically you say "if the length is zero and the length is 11 or more, then...".
Most likely you wish instead to use a disjunction, that is, to use or instead of and:
If (PlayerName = '') Or (Length(PlayerName) > 10) Then
This will display the error message if the name is empty or if it is too long.
In addition, the loop will exit even if the name is invalid, because if PlayerName is equal to ThisIsATooLongName then indeed PlayerName <> ''.
What you need is something like
Function GetValidPlayerName : String;
Var
PlayerName : String;
Begin
Repeat
Readln(PlayerName);
If (PlayerName = '') Or (Length(PlayerName) > 10) Then
Begin
Write('That was not a valid name. Please try again: ');
PlayerName := '';
End;
Until PlayerName <> '';
GetValidPlayerName := PlayerName;
End;
or
Function GetValidPlayerName : String;
Var
PlayerName : String;
Begin
result := '';
Repeat
Readln(PlayerName);
If (PlayerName = '') Or (Length(PlayerName) > 10) Then
Write('That was not a valid name. Please try again: ')
Else
result := PlayerName;
Until result <> '';
End;
Urm Im in a similar situation,
while(Length(conversionrates[i].rate)<>2)) do
begin
writeln('the conversion name should be 2 letters. (E.G Pounds to Dollars would be "PD")');
readln(conversionrates[i].fromto);
end;
Wondering if this would work, the program I put this is wont compile.