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
SimpleWater
Posts: 94
Joined: Tue 19 Apr 2011, 11:53

perl problems

#1 Post by SimpleWater »

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?

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

perl quiz

#2 Post by L18L »

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 :arrow: BaCon

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

#3 Post by SimpleWater »

I did try chop and chomp, and i got the same results :?

2lss
Posts: 225
Joined: Sun 20 Sep 2009, 23:54

#4 Post by 2lss »

Do you know what variable type stdin represents? i.e. integer, or string?

Since "sahara" is quoted, it probably represents a string in the eyes of the interpreter.

You could be comparing two different types of variables.

But I know little about perl so its just a guess.

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

perl quiz

#5 Post by L18L »

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 :)

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

Re: perl quiz

#6 Post by SimpleWater »

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 :)
:P I guess you expect me to learn a whole new language eh? I have done multiple searches on chop though.

hello 2lss,

I had a hard time following, i know it is an alphabetical string. Now what?

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

Re: perl quiz

#7 Post by L18L »

SimpleWater wrote: I guess you expect me to learn a whole new language eh? I have done multiple searches on chop though.
Wrong guess. You want learn a whole new language :lol:

http://www.wellho.net/mouth/255_STDIN-S ... ndles.html

http://www.wellho.net/mouth/2963_Removi ... ence-.html

That is not
tons of tutorials across the net
just 2 clicks

Good luck :wink:

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.

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

#8 Post by technosaurus »

shell is so much easier for me ... maybe someone could translate it to perl

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.
testfile should be in the working directory (unless you include the full path) and in the format:
  • 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].

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

#9 Post by SimpleWater »

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:

Code: Select all

5+5
bash:

Code: Select all

((5+5))

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

simple quiz

#10 Post by L18L »

technosaurus wrote:shell is so much easier for me ... maybe someone could translate it to perl
SimpleWater,
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
>>>

:)

User avatar
technosaurus
Posts: 4853
Joined: Mon 19 May 2008, 01:24
Location: Blue Springs, MO
Contact:

Re: simple quiz

#11 Post by technosaurus »

L18L wrote:SimpleWater,
technosaurus did not say that anything is easy in shell but it is easier for him
That's exactly what I meant. I haven't _had_ to use perl, so I don't.

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].

akash_rawal
Posts: 229
Joined: Wed 25 Aug 2010, 15:38
Location: ISM Dhanbad, Jharkhand, India

#12 Post by akash_rawal »

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";}


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

simple quiz

#13 Post by L18L »

akash_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";}

+1 :)
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:
Bruce wrote I dont remember where but he wrote:One idea leads to another..
- Code a quiz in sh, bash4, perl, python,....,C, BaCon
- 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...
:arrow: Puppy Certified [User|Administrator|Developer|Maintainer|Promoter|Tester|Translator|Troll] :wink: :roll: :idea:
Am I joking or not joking? That is the question.

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

#14 Post by SimpleWater »

akash_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";}

Correct! :D Believe me there was plenty of trial going on here, but i guess that's not enough for someone with minimal experience. Thanks

L18L, interesting, but overwhelming. One language is trivial enough for me

akash_rawal
Posts: 229
Joined: Wed 25 Aug 2010, 15:38
Location: ISM Dhanbad, Jharkhand, India

#15 Post by akash_rawal »

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.

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