LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-30-2011, 05:38 PM   #1
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7/10
Posts: 893

Rep: Reputation: 119Reputation: 119
perl programing looking for a review


Hi! I finally got time to get back to trying to learn perl and I did a number guessing game for practice and understanding and its a exercise to perform out of a book and I was wondering is there far better ways to write this code with only using

if, elsif, else, for, and while statments?

Code:
#!/usr/bin/perl -w

$number=int (rand 10);
print "Pick a number:";
$guess = <STDIN>;
chomp $guess;
while ($guess != $number){
if ($guess>$number){
    print "You guess to high!\n";
    print "Pick a number:";
    $guess = <STDIN>;
    chomp $guess;
    }
        
    elsif ($guess<$number){
        print "You guess to low!\n";
    print "Pick a number:";
    $guess = <STDIN>;
    chomp $guess;
    }}    

    if ($guess== $number) {
        print "You got it right!\n";
    }
Any help is appreciated.

Thanks
 
Old 10-30-2011, 05:51 PM   #2
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Maybe use an infinite loop, and exit when the user got it right

PHP Code:
#!/usr/bin/perl -w

$number=int (rand 10);

while( 
) {
  print 
"Pick a number:";
  
$guess = <STDIN>;
  
chomp $guess;

  if (
$guess>$number){
    print 
"You guess to high!\n";
    
next;
  }

  if(
$guess<$number) {
    print 
"You guess to low!\n";
    
next;
  }

  if (
$guess== $number) {
    print 
"You got it right!\n";
    
last;
  }

 
Old 10-31-2011, 10:37 AM   #3
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7/10
Posts: 893

Original Poster
Rep: Reputation: 119Reputation: 119
Very nicely done. I guess the more I learn I'll should be able to figure it out. Thanks
 
Old 10-31-2011, 11:48 AM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by jmc1987 View Post
Very nicely done. I guess the more I learn I'll should be able to figure it out. Thanks
You code doesn't have

Code:
use strict;
as the very first statement after the interpreter line.
 
Old 11-07-2011, 12:30 PM   #5
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7/10
Posts: 893

Original Poster
Rep: Reputation: 119Reputation: 119
I just wanted to give a update. Still working on learning and I was proud to say I actually rewrote it with out looking at the book just using knowledge I learned and I think believe I am progressing but with practice I will get more advanced perl programs.

PHP Code:
#!/usr/bin/perl -w

$number=int (rand 10);
$guesses=3;
$wrong=0;
$guess=();

while(
$wrong $guesses){
    print 
"Pick a Number 1-10.";
    
$guess=<STDIN>;
    if (
$guess != $number){
        
$wrong++;
        
$chancesleft=$guesses-$wrong;
        if (
$guess $number){
            print 
"WRONG!\n";
            print 
"You guessed to low! Please guess again!\n";
        
        }
        if (
$guess $number) {
            print 
"WRONG!\n";
            print 
"You guessed to high! Please guess again!\n";
            
        }
        print 
"You have $chancesleft Chances Left!\n\n";
        
    } else {
        print 
"Great Job!  You Guessed it Right!\n";
        exit;
    }
}
print 
"Thanks for Playing, Better luck next time!\n"
I know its still not much but I'm trying to get it down.

But thanks all for helping. Once I master this I plan to contribute to the Open source community.

Last edited by jmc1987; 11-07-2011 at 01:16 PM.
 
Old 11-07-2011, 07:17 PM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by jmc1987 View Post
I just wanted to give a update. Still working on learning and I was proud to say I actually rewrote it with out looking at the book just using knowledge I learned and I think believe I am progressing but with practice I will get more advanced perl programs.

PHP Code:
#!/usr/bin/perl -w

$number=int (rand 10);
$guesses=3;
$wrong=0;
$guess=();

while(
$wrong $guesses){
    print 
"Pick a Number 1-10.";
    
$guess=<STDIN>;
    if (
$guess != $number){
        
$wrong++;
        
$chancesleft=$guesses-$wrong;
        if (
$guess $number){
            print 
"WRONG!\n";
            print 
"You guessed to low! Please guess again!\n";
        
        }
        if (
$guess $number) {
            print 
"WRONG!\n";
            print 
"You guessed to high! Please guess again!\n";
            
        }
        print 
"You have $chancesleft Chances Left!\n\n";
        
    } else {
        print 
"Great Job!  You Guessed it Right!\n";
        exit;
    }
}
print 
"Thanks for Playing, Better luck next time!\n"
I know its still not much but I'm trying to get it down.

But thanks all for helping. Once I master this I plan to contribute to the Open source community.
By not using 'strict' you invite a lot of trouble - which otherwise would have been reported by the compiler. You also program using only global variables - which again is not a good idea.

Realize what 'my' keyword in Perl is for, and what is lexical scoping: http://en.wikipedia.org/wiki/Scope_(programming) .

So far you are making progress in gaining bad programming habits - even if your code works as expected.
 
1 members found this post helpful.
Old 11-07-2011, 08:04 PM   #7
jmc1987
Member
 
Registered: Sep 2009
Location: Oklahoma
Distribution: Debian, CentOS, windows 7/10
Posts: 893

Original Poster
Rep: Reputation: 119Reputation: 119
Yes I am aware of them being global but I was not worried about it considered it is a small program and I don't plan to do further development. But with bigger programs I put variables in blocks. I was just mainly showing that I am writing a bit more cleaner and readable code. But still much room for improvement 1 step at a time. I am going to start using strict though so I does help me build cleaner programs.
 
Old 11-07-2011, 09:42 PM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,671
Blog Entries: 4

Rep: Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945Reputation: 3945
Proceed, proceed...

The first computer program I ever wrote was eight lines long (in BASIC), took me six months to write, and had a bug in it.

I learned the craft by writing literally hundreds of programs. I was fascinated (and I still am ...) by how to make a machine do useful things. I'm also pleased to have the opportunity every day to spend time in the company of others who share my same fascination and addiction. May it never "wear off."

The beauty of computer programming is that there are no "rules." There are plenty of very good guidelines, and lots of war-story experience, but at the end of the day it is just you and the machine. It can go anywhere you can figure out how to take it, and can do anything you can figure out how to make it do. Plus, you can get paid for having fun! (If you don't go crazy first.)
 
1 members found this post helpful.
Old 11-08-2011, 07:17 AM   #9
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Sergei Steshenko View Post
You also program using only global variables - which again is not a good idea.
In this program, global variables are adequate though.
 
Old 11-08-2011, 02:59 PM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Cedrik View Post
In this program, global variables are adequate though.

I think I have already written it here - when one "studies" choreography, the tutors do not first teach to dance, they first teach choreographic positions, i.e. how to position hands, feet, shoulders - this is "static"; they teach movements - this is "dynamic", and only then they teach to dance - based on "static" and "dynamic".

My point is that getting rid of bad habits is much more difficult than net acquiring them in the first place.
 
Old 11-08-2011, 03:18 PM   #11
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by Sergei Steshenko View Post
I think I have already written it here - when one "studies" choreography, the tutors do not first teach to dance, they first teach choreographic positions, i.e. how to position hands, feet, shoulders - this is "static"; they teach movements - this is "dynamic", and only then they teach to dance - based on "static" and "dynamic".

My point is that getting rid of bad habits is much more difficult than net acquiring them in the first place.
Just curious, could you re-write this script without any global variable ?
 
Old 11-08-2011, 03:51 PM   #12
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Cedrik View Post
Just curious, could you re-write this script without any global variable ?
I can rewrite it with top level lexical variables, and they are different from global ones, e.g. can not be modified by a require'd file.

Also, the actual executable code can be enveloped into a subroutine, and the subroutine can be anonymous, e.g.

Code:
my $do_the_job_sub =
sub
  {
  # actual code goes here
  };

$do_the_job_sub->(); # the job is actually done by this invocation
- since the code is in the subroutine, the variables won't even be top level lexical ones.

The anonymous subroutine can not be overridden - unlike classical named subroutines,

Or the actual executable code can just be enveloped into an additional scope:

Code:
  {
  # actual code goes here
  }
- again, the variables won't even be top level lexical ones.
 
1 members found this post helpful.
Old 11-08-2011, 04:09 PM   #13
Cedrik
Senior Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 2,140

Rep: Reputation: 244Reputation: 244Reputation: 244
Ok, you can
But what would be the advantage of making the data privates in this program ?
I mean, why overcomplicate things when acheiving simple tasks ?

I can see the advantage in larger program where you want to keep each process separate, but here there is not much to do
 
Old 11-08-2011, 04:13 PM   #14
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by Cedrik View Post
Ok, you can
But what would be the advantage of making the data privates in this program ?
I mean, why overcomplicate things when acheiving simple tasks ?

I can see the advantage in larger program where you want to keep each process separate, but here there is not much to do
As I said, the goal is having the right habits. The amount of global variables should be kept to bear minimum.

Encapsulation is a right habit - not having global variables is the first step on the path to encapsulation.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Book Review: Learning Perl 6th Edition LXer Syndicated Linux News 0 09-07-2011 12:20 AM
LXer: Mini Review: Open Source inHarvard Business Review LXer Syndicated Linux News 0 05-02-2008 05:10 AM
perl programing mobs99 Linux - Newbie 7 10-27-2007 10:36 AM
LXer: Review: Mastering Perl LXer Syndicated Linux News 0 08-28-2007 06:21 AM
LXer: Book review: Wicked Cool Perl Scripts by Steve Oualline LXer Syndicated Linux News 0 04-20-2006 01:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration