perl problems

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

simple quiz

#16 Post by L18L »

technosaurus wrote:shell is so much easier for me ... maybe someone could translate it to perl
...
  • what is 1+1|2
    what is the square root of 9|3
translated to BaCon

Code: Select all

'#!/bin/sh
'# technosaurus http://www.murga-linux.com/puppy/viewtopic.php?t=67704
' commented sh lines by apostroph in this 1st BaCon version by L18L

'#initialize variables (just in case)
numright=0
'questions=0

'ask() {  ##usage: ask "What is your question?" "correct answer"
FUNCTION ask(STRING q$, STRING a$)

 LOCAL response$
'#ask question and wait for an answer feel free to replace read with dialog/xdialog
'read -p "$1 ?  " response
 INPUT CONCAT$(q$, " ?  "), response$
'#put in all lower case
'answer=`echo $2 |tr [:upper:] [:lower:]`
 a$ = LCASE$(a$)
'response=`echo $response |tr [:upper:] [:lower:]`
 response$ = LCASE$(response$)

'#check ans
'[ "$response" == "$answer" ] && echo correct && numright=$(($numright+1)) || echo incorrect
 IF response$ != a$ THEN RETURN "incorrect"
 INCR numright
 RETURN "correct"

END FUNCTION
'}

'IFS="
'"

OPEN "testfile" FOR READING AS myfile
'for oneline in `cat testfile`; do
WHILE NOT(ENDFILE(myfile)) DO
 READLN oneline$ FROM myfile
 SPLIT oneline$ BY "|" TO quiz$ SIZE questions
'  question=`echo $oneline |cut -d "|" -f 1`
 question$ = quiz$[0]
'  answer=`echo $oneline |cut -d "|" -f 2`
 answer$ = quiz$[1]
'  ask "$question" "$answer"
 response$ = ask(question$, answer$)
 PRINT response$
'  questions=$(($questions+1))
'done
WEND
CLOSE FILE myfile

'echo you got $numright out of $questions correct.
PRINT numright, questions FORMAT "you got %1$d out of %2$d Questions correct."

testfile:

Code: Select all

What is the largest desert in the world|Sahara
what is 1+1|2
what is the square root of 9|3
Quirky detail: testfile mustnot have a blank line at the end
but bacon source must have a blank line at the end :)

Edited:
Load above BaCon code into a file, say quiz.bac in directory $HOME/programming/bacon
Get syntax Highlighting in geany by
Document > Set Filetype > Programming Languages > FreeBasic source file
(That is how I use it, there are better ways to do it e.g. NicoEdit see http://bkhome.org/bacon/gettingstarted.htm )

Dud
Posts: 59
Joined: Sat 04 Jun 2011, 18:17

Code suggestions

#17 Post by Dud »

L18L was right to ask about line endings but akash_rawal's solution is, er, <shudder>, inelegant.

Here's an extended version of your script that demonstrates what's going on and uses chomp (intelligent chop) to remedy the problem. It also moves your check to a subroutine for multiple use.

#!/usr/bin/perl
use strict;
use warnings;
use 5.010;

print "What is the largest desert in the world? ";
my $reply=<stdin>;
say "\$reply contains: [$reply]";
saharacheck();

chomp ($reply);
say "After chomp \$reply contains: [$reply]";
saharacheck();

print "OK, this is another way to do it:\nWhat is the largest ocean in the world? ";
$reply=<stdin>;
if ($reply =~ /pacific/i){
say "correct!";
} else {
say "incorrect";
}

exit(0);

sub saharacheck{
print "Using old comparison: ";
if ($reply eq "sahara") {say "correct!";}
else {say "incorrect";}
}


Now here's how I might approach the task with more functionality, 'though this is left a little long-winded to aid comprehension:

#!/usr/bin/perl
use strict;
use warnings;
use 5.010;

question("What is the largest desert in the world?","sahara");
question("What is the largest ocean in the world?","pacific");

exit(0);

sub question{
my ($q, $a)=@_;
state ($right, $count);
$right+=0; # initialise in case the first answer is wrong
$count++;
print "$q ";
my $reply=<stdin>;
if ($reply =~ /$a/i) { # case insensitive regex match
print "Correct";
$right++;
} else {
print "Wrong";
}
print ", the answer is \u$a. Score: $right/$count\n";
}


# ./quiz.pl
What is the largest desert in the world? It's Sahara, I think.
Correct, the answer is Sahara. Score: 1/1
What is the largest ocean in the world? pacIFIC
Correct, the answer is Pacific. Score 2/2

Hth.

SimpleWater
Posts: 94
Joined: Tue 19 Apr 2011, 11:53

#18 Post by SimpleWater »

akash_rawal wrote:
SimpleWater wrote: Believe me there was plenty of trial going on here, but i guess that's not enough for someone with minimal experience.
I am not one with minimal programming knowledge, I was aware of the trailing newline problem because in C, almost like in this case, function getline() returns a line of text from given stream with a trailing newline which has to be removed manually.
I am sure you know much about programming. You see, that comment is towards me and not you, yes?

Yes i know how essential subroutines are Dud. Good for you to mention them, i see how i can optimize the code.

L18L, the name bacon just sounds interesting and also amusing :lol:

User avatar
L18L
Posts: 3479
Joined: Sat 19 Jun 2010, 18:56
Location: www.eussenheim.de/

simple quiz

#19 Post by L18L »

SimpleWater,
so you are motivated now to try your simple quiz in perl using a function ask and take the questions/answers from a text file? :)

BTW: Before I have never associated bacon other than "Sir Francis B."

Post Reply