perl problems
-
- Posts: 94
- Joined: Tue 19 Apr 2011, 11:53
perl problems
So i have been reading tons of tutorials across the net. Anyways, i would like to request that someone create a thread dedicated to perl questions, as i get stuck very often :p
While i was trying to see if i can make a very simple quiz, it fails. why?
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
print "What is the largest desert in the world? ";
if (<stdin>eq"sahara") {say "correct!";}
else {say "incorrect";}
why is it when i am asked the question, and type in exactly sahara, i get incorrect always. sahara is the correct answer. Can someone help me with the syntax?
While i was trying to see if i can make a very simple quiz, it fails. why?
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
print "What is the largest desert in the world? ";
if (<stdin>eq"sahara") {say "correct!";}
else {say "incorrect";}
why is it when i am asked the question, and type in exactly sahara, i get incorrect always. sahara is the correct answer. Can someone help me with the syntax?
- L18L
- Posts: 3479
- Joined: Sat 19 Jun 2010, 18:56
- Location: www.eussenheim.de/
perl quiz
Are you sure your input is exactly sahara ?
Maybe there is trailing something like newline.
Try to chop the input.
Hope that helps
Note, I have never used or learnt pearl.
But the same has to be done in BaCon
Maybe there is trailing something like newline.
Try to chop the input.
Hope that helps
Note, I have never used or learnt pearl.
But the same has to be done in BaCon
-
- Posts: 94
- Joined: Tue 19 Apr 2011, 11:53
- L18L
- Posts: 3479
- Joined: Sat 19 Jun 2010, 18:56
- Location: www.eussenheim.de/
perl quiz
try this:
http://www.lehrer.uni-karlsruhe.de/~za1 ... perl01.htm
found this by searching: perl chop stdin
Your result might vary dependent of your language
http://www.lehrer.uni-karlsruhe.de/~za1 ... perl01.htm
found this by searching: perl chop stdin
Your result might vary dependent of your language
-
- Posts: 94
- Joined: Tue 19 Apr 2011, 11:53
Re: perl quiz
I guess you expect me to learn a whole new language eh? I have done multiple searches on chop though.L18L wrote:try this:
http://www.lehrer.uni-karlsruhe.de/~za1 ... perl01.htm
found this by searching: perl chop stdin
Your result might vary dependent of your language
hello 2lss,
I had a hard time following, i know it is an alphabetical string. Now what?
- L18L
- Posts: 3479
- Joined: Sat 19 Jun 2010, 18:56
- Location: www.eussenheim.de/
Re: perl quiz
Wrong guess. You want learn a whole new languageSimpleWater wrote: I guess you expect me to learn a whole new language eh? I have done multiple searches on chop though.
http://www.wellho.net/mouth/255_STDIN-S ... ndles.html
http://www.wellho.net/mouth/2963_Removi ... ence-.html
That is not
just 2 clickstons of tutorials across the net
Good luck
Edited: You might ask here more specific
Did you read "chop-or-chomp-in-Perl-what-is-the-difference"
Reproduce that example?
Last edited by L18L on Fri 13 May 2011, 14:51, edited 1 time in total.
- technosaurus
- Posts: 4853
- Joined: Mon 19 May 2008, 01:24
- Location: Blue Springs, MO
- Contact:
shell is so much easier for me ... maybe someone could translate it to perl
testfile should be in the working directory (unless you include the full path) and in the format:
Code: Select all
#!/bin/sh
#initialize variables (just in case)
numright=0
questions=0
ask() { ##usage: ask "What is your question?" "correct answer"
#ask question and wait for an answer feel free to replace read with dialog/xdialog
read -p "$1 ? " response
#put in all lower case
answer=`echo $2 |tr [:upper:] [:lower:]`
response=`echo $response |tr [:upper:] [:lower:]`
#check ans
[ "$response" == "$answer" ] && echo correct && numright=$(($numright+1)) || echo incorrect
}
IFS="
"
for oneline in `cat testfile`; do
question=`echo $oneline |cut -d "|" -f 1`
answer=`echo $oneline |cut -d "|" -f 2`
ask "$question" "$answer"
questions=$(($questions+1))
done
echo you got $numright out of $questions correct.
- what is 1+1|2
what is the square root of 9|3
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].
-
- Posts: 94
- Joined: Tue 19 Apr 2011, 11:53
I can just leave this alone for now. Not all things are easier in the shell though, but i can agree on most. For example math:
perl:
bash:
perl:
Code: Select all
5+5
Code: Select all
((5+5))
- L18L
- Posts: 3479
- Joined: Sat 19 Jun 2010, 18:56
- Location: www.eussenheim.de/
simple quiz
SimpleWater,technosaurus wrote:shell is so much easier for me ... maybe someone could translate it to perl
technosaurus did not say that anything is easy in shell but it is easier for him
Anyway math in python is easy:
# python
Python 2.6.4 (r264:75706, Sep 13 2010, 13:05:58)
[GCC 4.3.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 5+5
10
>>>
- technosaurus
- Posts: 4853
- Joined: Mon 19 May 2008, 01:24
- Location: Blue Springs, MO
- Contact:
Re: simple quiz
That's exactly what I meant. I haven't _had_ to use perl, so I don't.L18L wrote:SimpleWater,
technosaurus did not say that anything is easy in shell but it is easier for him
btw here is the awk based function I normally use for doing in-shell calculations:
calc() {
awk "BEGIN{print $@}"
}
+,-,*,/,%,sqrt() ... any math functions that awk supports (and it can be a rather large set)
in fact here is the stupidest little calculator I've created
Code: Select all
#!/bin/sh
A=`awk "BEGIN{print $@}"`
A=`Xdialog --stdout --inputbox "Simple calculator" 0 0 $A`
[ $A ] && $0 $A
Check out my [url=https://github.com/technosaurus]github repositories[/url]. I may eventually get around to updating my [url=http://bashismal.blogspot.com]blogspot[/url].
-
- Posts: 229
- Joined: Wed 25 Aug 2010, 15:38
- Location: ISM Dhanbad, Jharkhand, India
I have no knowledge of perl, but by trial I found the fix.
I was sure that the trailing newline is the issue.
I was sure that the trailing newline is the issue.
Code: Select all
#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
print "What is the largest desert in the world? ";
if (<stdin>eq"sahara
") {say "correct";}
else {say "incorrect";}
- L18L
- Posts: 3479
- Joined: Sat 19 Jun 2010, 18:56
- Location: www.eussenheim.de/
simple quiz
+1akash_rawal wrote:I have no knowledge of perl, but by trial I found the fix.
I was sure that the trailing newline is the issue.Code: Select all
#!/usr/bin/perl use strict; use warnings; use 5.010; print "What is the largest desert in the world? "; if (<stdin>eq"sahara ") {say "correct";} else {say "incorrect";}
Now it will need just a chop so that it can be used in technosaurus' predefined list of question|answer.
---------------------------------------------what is 1+1|2
what is the square root of 9|3
Edited:
- Code a quiz in sh, bash4, perl, python,....,C, BaConBruce wrote I dont remember where but he wrote:One idea leads to another..
- this on console and gui
- Use one an the same list of question|answers
- internationalization using GNU gettext
- localization (translating to human languages)
Learning from the different programming languages.
Take puppy related questions...
Puppy Certified [User|Administrator|Developer|Maintainer|Promoter|Tester|Translator|Troll]
Am I joking or not joking? That is the question.
-
- Posts: 94
- Joined: Tue 19 Apr 2011, 11:53
Correct! Believe me there was plenty of trial going on here, but i guess that's not enough for someone with minimal experience. Thanksakash_rawal wrote:I have no knowledge of perl, but by trial I found the fix.
I was sure that the trailing newline is the issue.Code: Select all
#!/usr/bin/perl use strict; use warnings; use 5.010; print "What is the largest desert in the world? "; if (<stdin>eq"sahara ") {say "correct";} else {say "incorrect";}
L18L, interesting, but overwhelming. One language is trivial enough for me
-
- Posts: 229
- Joined: Wed 25 Aug 2010, 15:38
- Location: ISM Dhanbad, Jharkhand, India
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.SimpleWater wrote: Believe me there was plenty of trial going on here, but i guess that's not enough for someone with minimal experience.
- L18L
- Posts: 3479
- Joined: Sat 19 Jun 2010, 18:56
- Location: www.eussenheim.de/
simple quiz
translated to BaContechnosaurus 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
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."
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
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 )
Code suggestions
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.
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.
-
- Posts: 94
- Joined: Tue 19 Apr 2011, 11:53
I am sure you know much about programming. You see, that comment is towards me and not you, yes?akash_rawal wrote: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.SimpleWater wrote: Believe me there was plenty of trial going on here, but i guess that's not enough for someone with minimal experience.
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
- L18L
- Posts: 3479
- Joined: Sat 19 Jun 2010, 18:56
- Location: www.eussenheim.de/
simple quiz
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."
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."