Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Your first Perl script

Programming languages, Coding, Executables, Package Creation, and Scripting.
Message
Author
User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Your first Perl script

#1 Post by GarryRicketson »

Edited: New title, "your first perl script"
When I started the topic, I had not intended it to be just for trying to get help on my first Perl script, but also a topic where others can share theirs as well and ask any questions
they may have. So any way, it dawned on me the title needed to be changed from
"My" to yours or "our", ??
So any way, with that said. Below is my first script.
--------- end edited-------------------

I have been aware of Perl for several years, and seen it used a lot , especially in
Debian and other Linux systems, as well as unix. So any way, I looked at some tutorials
today, and got started on this:

Code: Select all

#!/usr/bin/perl
#use strict;
#use warnings;
print "Times tables 1 through 10\n";
print " 3 x 5 = type answer: ";
$answer = <STDIN>;
chomp $answer;
while ( $answer ne "15"  ) {
	print "$answer is not correct try again: ";
	$answer = <STDIN>;
	chomp $answer;
}
print "Great  $answer is correct.\n";
print "Hit enter key to continue: ";
$_ = <STDIN>;
if ( $answer eq "15" ) {
	print "15 is correct\n";
}
#else {(this is not doing anything)
#	print "Wrong !the correct answer is 15\n";
#}(besides that, maybe I do not want to give the correct answer
#that easy. Go on to next problem.
#note : I read that this may not work on some sytems to clear the
#screen but it does on mine.
print "\033[2J";
print " 5 x 5 = type answer: ";
$answer = <STDIN>;
chomp $answer;
while ( $answer ne "25"  ) {
	print "$answer is not correct try again: ";
	$answer = <STDIN>;
	chomp $answer;
}
print "Great  $answer is correct.\n";
print "Hit enter key to continue: ";
$_ = <STDIN>;
if ( $answer eq "25" ) {
	print "25 is correct\n";
}
# goes on to next problem-------------
print "\033[2J";
print " 2 x 3 = type answer: ";
#The <STDIN> is the way to read keyboard input
$answer = <STDIN>;
chomp $answer;
while ( $answer ne "6"  ) {
	print "$answer is not correct try again: ";
	$answer = <STDIN>;
	chomp $answer;
}
print "Great  $answer is correct.\n";
print "Hit enter key to continue: ";
$_ = <STDIN>;
if ( $answer eq "6" ) {
	print "6 (six) is correct\n";
}
#On to next problem, I have decided to include the number in text
#as well, to help her learn the numbers in english.
 
It is going to be for my grandaughter, I know, there is "Tux Math" and it is great for kids,
but it is more fun to try to make my own.
I did do one using Qbasic, on Dos years ago that is pretty neat, I still have it and can run it on dosbox, however I would like to try to do this with Perl. I like perl so far, I think I can learn to work with it.
Similar, with the first qbasic script, it was over 100 lines of code, but when I shared it,
some body else showed me a way to do it with just a few lines code, that generated the numbers 1 through 10, in pairs randomly, then if the correct answer was entered
it genarated a new pair IE: 2 x 4=, .
I can make this work, like it is, with just a new problem, one after another, using the same code, but it would be well over 500 lines of code, just to cover the multiplication tables, 1 through 10. And it would not be random. So anyway, any ideas or examples would be welcome, how ever there are lots of tutorials I can keep reading as well, and maybe I will get some better idea.
Any way, just wanted to share this, trying it with bash I never even got this far, I like Perl.
Last edited by GarryRicketson on 2016-02-14 21:03, edited 3 times in total.

User avatar
dasein
Posts: 7680
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: My first Perl script

#2 Post by dasein »

https://lmsptfy.com/?q=random+numbers+in+perl :mrgreen:

You already identified the most important improvement you can make: modularize your code and use an RNG to eliminate gratuitous duplication. Now all you have to do is implement them.

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: My first Perl script

#3 Post by GarryRicketson »

Thanks, I was just reading some things, in the perl docs, manual as well.
About "methods", classes, functions. etc...
I should have thought of that "random numbers in perl", and there is several that look
good, hopefull, for tomorrow, I am to tired now, can't hardly see straight.
Wish I had started learning this 20 years ago, any way, I am glad now I have plenty of free time to start studying it.

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: My first Perl script

#4 Post by GarryRicketson »

Thanks again Dasein, this morning I found something I think I can work with:
Looks like it will work,

Code: Select all

garry@debian:~$ perl
my $random = int rand 10;
my $random2 = int rand 10;
print "$random x $random2 = these are the numbers";
$_ = <STDIN>;

8 x 8 = these are the numbers
garry@debian:~$   
Below, just to demonstrate how important typing accurate is important,
at first I though it wouldn't work, but look at how I spelled "random"!

Code: Select all

 garry@debian:~$ perl
my $random = int rand 10;
my $random2 = int rand 10;
print "$random x $randam2 = these are the numbers";
$_ = <STDIN>;
5 x  = these are the numbers
 

User avatar
golinux
Posts: 1579
Joined: 2010-12-09 00:56
Location: not a 'buntard!
Been thanked: 1 time

Re: My first Perl script

#5 Post by golinux »

You might want to have a look at this. Entertaining and informative info about perl.
May the FORK be with you!

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: My first Perl script

#6 Post by GarryRicketson »

@GOLINUX
Thanks,

User avatar
golinux
Posts: 1579
Joined: 2010-12-09 00:56
Location: not a 'buntard!
Been thanked: 1 time

Re: My first Perl script

#7 Post by golinux »

Do report back after you have a look.
May the FORK be with you!

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: My first Perl script

#8 Post by GarryRicketson »

golinux wrote:Do report back after you have a look.
Ok, well I looked, but not sure what to think or say, I am deaf , all though I have sound working but it is use less to me, I can't hear anything, so about all I could do is look, videos and such are not good learning tools for me, others seem to enjoy them though, The one guy looks like he has been using crack for to long, the other guy , well,...they are good actors. I don't know enough about perl to be able to determine if they can be taken seriously, but have my doubts.
But any way , thank you for sharing it, it is humorous any way. "Entertaining ",..well for some people it would be, but for me, something I can read, is more entertaining, and more chance I could learn from it. Thanks for sharing it though, others may very well enjoy it.
It appeared to be they are trying to say "Perl" is dead, and not usefull, I do not agree with that, but every one is entitled to believe or think what they want. One thing, the CGI junk is a non issue for me.
My interests are not in doing everything "online" , but being able to write scripts or programs I can use on my "offline system", for my own, or my families personal use and enjoyment.

User avatar
dasein
Posts: 7680
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: My first Perl script

#9 Post by dasein »

GarryRicketson wrote:Thanks again Dasein, this morning I found something I think I can work with:
Looks like it will work,

Code: Select all

garry@debian:~$ perl
my $random = int rand 10;
my $random2 = int rand 10;
Actually, based on your requirements spec, you're going to discover a subtle problem with this code as written. Yes, I could tell you what it is, but we all know that I'm a big fan of learning-by-doing, so I'm only going to give you a hint. It's a common enough mistake to have it's own name, and even acronym: it's called the off-by-one (OBO) bug.

Speaking of hints, I want to mention one other small point, more pertinent to learning than to code: Your script includes an embedded comment regarding whether to offer the answer right away when your granddaughter gets it wrong.

The pedagogue in me applauds you for wanting to encourage her to try again; but at the same time, the UX wonk in me wants to remind you not to "trap" her in a dead end if she doesn't know an answer. Consider either adding a wrong answer counter that gives the answer after a few failed attempts (an ok enough solution), or offering a "hint" option that, if she selects it, shows her "surrounding" answers (my personal preference). (You could also do both, I suppose.)

There are at least two good reasons to like the latter solution. I'll let you discover them on your own.

Explore well and learn much.

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: My first Perl script

#10 Post by GarryRicketson »

Consider either adding a wrong answer counter that gives the answer after a few failed attempts (an ok enough solution), or offering a "hint" option that, if she selects it, shows her "surrounding" answers (my personal preference). (You could also do both, I suppose.)
Thanks, I am still thinking about that, Seems like on the qbasic one , I had it give the correct answer, and then it gave the same problem over again and the user had to enter the correct answer.
Actually, based on your requirements spec, you're going to discover a subtle problem with this code as written. ---- It's a common enough mistake to have it's own name
Ok, I was reading some things about that last night, and had a feeling, I still have not tried much with the code , as it is written, and combined with the other code,...thanks for the "warning" .

User avatar
dasein
Posts: 7680
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: My first Perl script

#11 Post by dasein »

GarryRicketson wrote: It appeared to be they are trying to say "Perl" is dead, and not usefull...
Perl is a world all its own. For brute-force text processing, I know of nothing better. But True Perl Mastery requires a unique mindset and an ability to think in regular expressions.

Really long aside: I was once asked by a professor to write a suffix stripper (aka a stemmer) in perl. Now, I know enough to know that my perl looks more like 'c' than perl. I don't think in regex, and I am in no way a perl Master. So, before I started coding, I checked the perl archives, and sure enough, the code in question had (a) already been written and (b) had been written by someone whose perl skills were way better than mine. The code from the archive was short, readable, and brutally efficient. It handled every test case I could throw at it without error.

Next day, I delivered the program to the professor, and he was all impressed, "You wrote this in a day??" I freely admitted that I had not, and explained how I had come into possession of the code in question. He acted like I had just invented code reuse, and spent the rest of the day marveling at my ingenuity. I kept trying to point out that I was in no way ingenious, but rather merely lazy.

The text corpus we were processing was huge (at least by the standards of the day), and I have no doubt that the well-written "perl-ish" code from the perl archive churned through the text way faster than anything I would have written. We set it to run overnight, and by the time we came in the next day, the entire corpus had been reduced to its root words. (This was in the era when CPU speed was measured in MHz, not Ghz, and multi-core CPUs were something that only supercomputers had, so running in one night was actually a bit of a feat.)

tl;dr: If one needs to process big chunks of text, perl FTW!

Edit: typos
Last edited by dasein on 2016-02-12 20:49, edited 2 times in total.

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: My first Perl script

#12 Post by GarryRicketson »

I just found something else:

Code: Select all

use strict;
use warnings; 
See my first post, I had added these to lines (above), but had not actually tried
the code after I added them, to my surprise, with those 2 lines it does not work,
so I commented them out.
If you remember, when I asked you if you knew much about bash, and you said you didn't, well it had not yet occured to me to ask about perl. Sounds like you do know something about perl.
Perl is a world all its own. For brute-force text processing, I know of nothing better. But True Perl Mastery requires a unique mindset and an ability to think in regular expressions.
I don't know that I will ever achieve " True Perl Mastery", how ever, after weeks of reading bash tutorials, and trying various things, I had still not gotten anywhere, and nothing was making sense to me.
Just in a short time, maybe a hour, looking at the

Code: Select all

 man perl 
and also couple of tutorials, and some other perl scripts and it makes a lot of sense to me, my wife is all ways saying " Why are you not a normal child ? " (¿Por qué no eres un niño normal?),.. Does this mean I have a "unique mindset" ? I don't know, but so far perl makes much more sense to me then bash. Still each one has it's purpose and uses.

User avatar
dasein
Posts: 7680
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: My first Perl script

#13 Post by dasein »

GarryRicketson wrote:Sounds like you do know something about perl.
Not really. I know just enough to know how little I know. I can read well-crafted perl code well enough, but mostly what I know is how lame my perl skills are.
GarryRicketson wrote:Still each one has it's purose and uses.
Perl is designed for text processing. Like most any scripting language, it has basic flow control, conditional execution, etc. But its true strength lies in its ability to process and munge text like nothing else. In many ways, perl is much more similar to awk than it is to bash.

As with so many things, Wikipedia provides a nice historical overview: https://en.wikipedia.org/wiki/Perl

On another matter:
GarryRicketson wrote:I just found something else:

Code: Select all

use strict;
use warnings; 
[snip]
after I added them, to my surprise, with those 2 lines it does not work,
The use strict syntax is perl's way of enforcing strong typing. Coming as you do from early dialects of BASIC, you are used to being able to implicitly declare a variable for use merely by using it. Think of "use strict" in perl as roughly equivalent to "option explicit" in BASIC.

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: My first Perl script

#14 Post by GarryRicketson »

Actually, based on your requirements spec, you're going to discover a subtle problem with this code as written.
Heh, heh.. I don't know if what I found is the "subtle" problem you are saying, it was not all that subtle, I should have seen it coming,..
I need to include some code that takes the 2 random numbers that get generated , multiplies them, and then uses that in the $answer, to compare with whatever the input is, and if it is correct (matches), say correct, if it does not match, keep using those 2 numbers, until the right answer is entered, or show the correct answer and the user enters it.

Code: Select all

 #!/usr/bin/perl
my $random = int rand 10;
my $random2 = int rand 10;
print "$random x $random2 = these are the numbers";
$_ = <STDIN>;
print "Times tables 1 through 10\n";
print "$random x $random2 = type answer: ";
#The <STDIN> is the way to read keyboard input
$answer = <STDIN>;
chomp $answer;
######while ( $answer ne "15"  ) {
	print "$answer is not correct try again: ";
	$answer = <STDIN>;
	chomp $answer;############# This needs to be fixed, here I all ready have "15" ################# in it, I need something, that takes $random and $random2, mutlitplies them,and uses the result, as the value in $answer ne "xx", also some "input" code##################### my wife interupted me#################!!!!!!!
}
print "Great  $answer is correct.\n";
print "Hit enter key to continue: ";
$_ = <STDIN>;
if ( $answer eq "15" ) {
	print "15 is correct\n";
} 
Any way, I will get back to this, my wife is being a real pain today!!!! :evil:

User avatar
dasein
Posts: 7680
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: My first Perl script

#15 Post by dasein »

GarryRicketson wrote:I don't know if what I found is the "subtle" problem you are saying...
Nope. Sorry. ;)

User avatar
golinux
Posts: 1579
Joined: 2010-12-09 00:56
Location: not a 'buntard!
Been thanked: 1 time

Re: My first Perl script

#16 Post by golinux »

dasein . . . did you watch the video? It is very entertaining!
May the FORK be with you!

User avatar
dasein
Posts: 7680
Joined: 2011-03-04 01:06
Location: Terra Incantationum

Re: My first Perl script

#17 Post by dasein »

golinux wrote:dasein . . . did you watch the video?
I confess that I didn't. Kinda up to my ass in alligators these days.

User avatar
golinux
Posts: 1579
Joined: 2010-12-09 00:56
Location: not a 'buntard!
Been thanked: 1 time

Re: My first Perl script

#18 Post by golinux »

dasein wrote:
golinux wrote:dasein . . . did you watch the video?
I confess that I didn't. Kinda up to my ass in alligators these days.
It's a perfect diversion for when you need a break and a good laugh!
May the FORK be with you!

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: My first Perl script

#19 Post by GarryRicketson »

by golinux »It's a perfect diversion for when you need a break and a good laugh!
I wish I could hear them, or it was "closed" captchined, I felt like I am missing the humor since I could not hear what they say.
================= any way ===========
This may give some people a "laugh" , it is very crude, but does work. I need to look
at some of the tutorial on "loops" now, unless someone can tell me ?
At this point I would just like it to repeat, and give another problem, keep repeating and just use ctr-l c to exit, when I am ready.

Code: Select all

 #!/usr/bin/perl
my $random = int rand 10;
my $random2 = int rand 10;
print "$random x $random2 = please hit enter then your answer";
$_ = <STDIN>;
$x = $random * $random2;
chomp $answer;
while ( $answer ne "$x"  ) {
	print "$answer enter the answer: ";
	$answer = <STDIN>;
	chomp $answer;
}
print "Great  $answer is correct.\n";
print "Hit enter key to continue: ";
$_ = <STDIN>;
if ( $answer eq "$x" ) {
	print "$x is correct\n";
}
##### would like it to just start over again, when I hit "enter" ($_ = <STDIN>)
 
So far I have not yet encounterd the "subtle" problem
It's a common enough mistake to have it's own name, and even acronym: it's called the off-by-one (OBO) bug.
I suppose I should try doing a search on that as well.
Thanks
p.s. as much as I enjoy figuring it on my own, any examples, or ideas certainly are welcome.

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: My first Perl script

#20 Post by GarryRicketson »

So far I have not yet encounterd the "subtle" problem
Never needed to do a search, I finally remembered ! In qbasic, there is a also a "off by one", factor, I never though of it as a "bug" , and I have a loop now also:

Code: Select all

#!/usr/bin/perl
for( ; ; )
{
my $random = int rand 11;
my $random2 = int rand 11;
print "$random x $random2 = please hit enter then your answer";
$_ = <STDIN>;
$x = $random * $random2;
chomp $answer;
while ( $answer ne "$x"  ) {
	print "$answer enter the answer: ";
	$answer = <STDIN>;
	chomp $answer;
}
print "Great  $answer is correct.\n";
print "Hit enter key to continue: ";
$_ = <STDIN>;
if ( $answer eq "$x" ) {
	print "$x is correct\n";
}
print "hit enter to continue, ctr-c to exit";
$_ = <STDIN>;
print "\033[2J";
#print "\033[2J"Might not work on some systems but it does on mine
}
 
With the "11" , now it includes 10's, in the random numbers :D
Still really rough or crude , but now I have something to work with, ....

Post Reply