CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Teaching myself some basic perl

 



bclark8923
Novice

Jun 17, 2009, 5:48 AM

Post #1 of 16 (2100 views)
Teaching myself some basic perl Can't Post

so i know c++ but need perl for a new job so im slowly teaching myself as of today got this bit of code




Code
   

#!/usr/bin/perl
use warnings;

#read in values
print"Enter First Number: ";
$first = <STDIN> ;
print"Enter Second Number: ";
$second = <STDIN> ;
print"Enter +, -, *, or /: ";
$fn = <STDIN> ;
print"Enter Third Number: ";
$third = <STDIN> ;

#vars
$temp = 0;
$plus = "+";
$sub = '-';
$mult = '*';
$div = '/';

#check for the operator
if($fn eq $plus) {
$temp = ($first + $second);
}
elsif($fn eq $sub) {
$temp = ($first - $second);
}
elsif($fn eq $mult) {
$temp = ($first * $second);
}
elsif($fn eq $div) {
$temp = ($first / $second);
}
else {
print"error";
exit();
}

#print statement
print"Does $first $fn $second equal $third? ";

if ($temp == $third) {
print"yes\n";
}
else{
print"no\n";
}



but i cant figure out how to get the elsif statements to work, always goes to error, what is the correct syntax?

as well when i input 4, 9, + , 13 for the input, the output looks like


Code
  Does 4  
+
9
equal 13
? yes



how would i go about having it print all on one line? or would that be because im using it in cmd that it does that


(This post was edited by bclark8923 on Jun 17, 2009, 5:55 AM)


FishMonger
Veteran / Moderator

Jun 17, 2009, 6:59 AM

Post #2 of 16 (2089 views)
Re: [bclark8923] Teaching myself some basic perl [In reply to] Can't Post

When you receive user input from the STDIN handle, the input is terminated with a "\n" char, which needs to be removed. Perl has a built-in chomp function that removes the line terminator.

Code
$first = <STDIN> ; 
chomp $first;


Or, you could combine that into 1 line.

Code
chomp($first = <STDIN>);



bclark8923
Novice

Jun 17, 2009, 7:08 AM

Post #3 of 16 (2087 views)
Re: [bclark8923] Teaching myself some basic perl [In reply to] Can't Post

ha, that fixed my eq and my output, THANKS!


FishMonger
Veteran / Moderator

Jun 17, 2009, 8:17 AM

Post #4 of 16 (2081 views)
Re: [bclark8923] Teaching myself some basic perl [In reply to] Can't Post

You're welcome.

BTW, even though the script is working, it does have some issues.
1) You need to add the strict pragma and create lexical vars by declaring them with the my keyword.

Code
use warnings; 
use strict;

#read in values
print"Enter First Number: ";
chomp(my $first = <STDIN>);


2) The separate vars for the operators is "ok", but it "forces" you to use that messy if/elsif/else block. You should use a hash instead of the separate vars.

Code
my %operators = ( '+' => 'add', 
'-' => 'subtract',
'*' => 'multiply',
'/' => 'divide',
);


Now, you can drop each of the elsif sections and have a simple if/else block.

Your script is a pretty standard homework assignment and I normally don't provide complete answers to homework assignments. However, I'll take you at your word that this is a self taught exercise and not a school homework assignment.


Code
#!/usr/bin/perl     

use warnings;
use strict;

print"Enter First Number: ";
chomp(my $first = <STDIN>);

print"Enter Second Number: ";
chomp(my $second = <STDIN>);

print"Enter +, -, *, or /: ";
chomp(my $operator = <STDIN>);

print"Enter Third Number: ";
chomp(my $third = <STDIN>);

my %operators = (
'+' => 'add',
'-' => 'subtract',
'*' => 'multiply',
'/' => 'divide',
);

if ( exists $operators{$operator} ) {

my $equation = $first . $operator . $second;
my $result = eval $equation;

print"Does $first $operator $second equal $third?\n";
print $result == $third ? "yes\n" : "no, it equals $result\n";
}
else {
print"error";
}



bclark8923
Novice

Jun 17, 2009, 8:26 AM

Post #5 of 16 (2078 views)
Re: [bclark8923] Teaching myself some basic perl [In reply to] Can't Post

O wow perl is pretty nifty, yeah im an intern at a company and my next rotation they work with perl scripts, so I'm just teaching my self the workings of perl and such w/ forums/internet guides, etc. ill implement that thing you showed me

btw, would u know any good guides to help me get into a bit more perl, syntax is a bit confusing compared to C++ lol


1arryb
User

Jun 17, 2009, 8:46 AM

Post #6 of 16 (2074 views)
Re: [bclark8923] Teaching myself some basic perl [In reply to] Can't Post

Hi bclark,

This thread: http://perlguru.com/gforum.cgi?post=38342;#38342 has a few pointers.

Cheers,

Larry


FishMonger
Veteran / Moderator

Jun 17, 2009, 8:54 AM

Post #7 of 16 (2072 views)
Re: [bclark8923] Teaching myself some basic perl [In reply to] Can't Post


In Reply To
btw, would u know any good guides to help me get into a bit more perl, syntax is a bit confusing compared to C++ lol


It depends on your style of learning preference. Perl comes with lots of documentation i.e. perldoc

From the command line:
[root@rkb-2 ~]# perldoc perl

Or on the net:
http://perldoc.perl.org/

If you like books, then you can get a brief listing from:
perldoc -q books

Here are a few that I recommend.
Learning Perl , Fifth Edition
http://oreilly.com/catalog/9780596520106/

Perl Best Practices
http://oreilly.com/catalog/9780596001735/

Programming Perl, Third Edition
http://oreilly.com/catalog/9780596000271/

Assortment of books
http://oreilly.com/pub/topic/perl


bclark8923
Novice

Jun 17, 2009, 9:15 AM

Post #8 of 16 (2068 views)
Re: [bclark8923] Teaching myself some basic perl [In reply to] Can't Post

well atleast while loops are pretty much the same as C++ ha


Code
 #!/usr/bin/perl   

use warnings;
use strict;

while(1){

print"Enter First Number: ";
chomp(my $first = <STDIN>) ;
if($first eq 'q'){exit();}


print"Enter Second Number: ";
chomp(my $second = <STDIN>) ;
if($second eq 'q'){exit();}


print"Enter +, -, *, or /: ";
chomp(my $fn = <STDIN>) ;
if($fn eq 'q'){exit();}


print"Enter Third Number: ";
chomp(my $third = <STDIN>) ;
if($third eq 'q'){exit();}


my %operators = ( '+' => 'add',
'-' => 'subtract',
'*' => 'multiply',
'/' => 'divide',
);


#check for the operator
if ( exists $operators{$fn} ) {

my $equation = $first . $fn . $second;
my $result = eval $equation;

print"Does $first $fn $second equal $third? ";
print $result == $third ? "Yes\n\n" : "No, it equals $result\n\n";
}
else {
print"error\n\n";
}
}



Anyone have any ideas of a simple program/script I could write, thats usually how i learn best, try to accomplish a goal and learn the syntax/how to use the language as i write it

i.e. this program taught me loop if/elsif and var/hash/array(from reading) syntax for perl


(This post was edited by bclark8923 on Jun 17, 2009, 9:25 AM)


1arryb
User

Jun 17, 2009, 10:03 AM

Post #9 of 16 (2058 views)
Re: [bclark8923] Teaching myself some basic perl [In reply to] Can't Post

Hi bclark,

You could do worse than surfing the beginner's forum and try to solve some of the problems presented there yourself.

Cheers,

Larry


KevinR
Veteran


Jun 17, 2009, 10:11 AM

Post #10 of 16 (2057 views)
Re: [bclark8923] Teaching myself some basic perl [In reply to] Can't Post


Quote
Anyone have any ideas of a simple program/script I could write, thats usually how i learn best, try to accomplish a goal and learn the syntax/how to use the language as i write it


Write a script that generates 100 unique strings of 7 random numbers using numbers 1 thru 50. ie:

2-7-44-21-8-5-10
15-23-39-44-33-1

A don't cheat by doing a google search for existing code that generates random strings of numbers.
-------------------------------------------------


FishMonger
Veteran / Moderator

Jun 17, 2009, 10:33 AM

Post #11 of 16 (2053 views)
Re: [bclark8923] Teaching myself some basic perl [In reply to] Can't Post

Try working on the assignments from the class I took.

http://krypton.fhda.edu/~gever/


bclark8923
Novice

Jun 17, 2009, 10:41 AM

Post #12 of 16 (2050 views)
Re: [KevinR] Teaching myself some basic perl [In reply to] Can't Post

when you say unique, do you mean the order of the numbers and numbers used? or just the numbers used?


(This post was edited by bclark8923 on Jun 17, 2009, 10:43 AM)


KevinR
Veteran


Jun 17, 2009, 10:44 AM

Post #13 of 16 (2046 views)
Re: [bclark8923] Teaching myself some basic perl [In reply to] Can't Post


In Reply To
when you say unique, do you mean the order of the numbers? or the numbers used?


Just that no two strings of numbers can be repeated in the list of 100 strings. After that you can try generating unique strings of unique numbers where no number can be repeated in the same string and no two strings in the list of 100 strings can be repeated.
-------------------------------------------------


bclark8923
Novice

Jun 17, 2009, 11:15 AM

Post #14 of 16 (2043 views)
Re: [KevinR] Teaching myself some basic perl [In reply to] Can't Post

heh so this is really sloppy and could prolly be more efficient but o well


Code
   
#!/usr/bin/perl

use warnings;
use strict;

my $x = 0;
my $y = 0;
my $z = 0;
my $range = 50;
my $random_number = 0;
my $recalculate = 1;
my @currstring;
$currstring[7] = (0);
my @numused;
$numused[50] = undef;
my @matrix;
$matrix[50][50][50][50][50][50][50] = undef;

while($x < 100){
$y = 0;
$recalculate = 1;
undef(@numused);
while($recalculate == 1){
while($y < 7){
$random_number = int(rand($range));
while($numused[$random_number]) {$random_number = int(rand($range));}
$currstring[$y] = $random_number;
$numused[$random_number] = 1;
$y++;
}
if(!$matrix[$currstring[0]][$currstring[1]][$currstring[2]][$currstring[3]][$currstring[4]][$currstring[5]][$currstring[6]]){
$matrix[$currstring[0]][$currstring[1]][$currstring[2]][$currstring[3]][$currstring[4]][$currstring[5]][$currstring[6]] = 1;
$recalculate = 0;
}
if($recalculate == 0){
$z = 0;
while($z < 6){
print $currstring[$z], "-";
$z++;
}
print $currstring[$z], "\n";
}
}
$x++;
}



basically picks a random number 0 to 50 7 times, then checks a huuuge array to see if that specific string has been used, if so recalculate, as well along the way it checks if the number pulled has been used in that string alrdy and if so recalculates a random number

then at the end of each calculation it outputs that string


edit: i really need to think about these better, ugly/not very dynamic heh, any suggestions to it?


(This post was edited by bclark8923 on Jun 17, 2009, 11:52 AM)


KevinR
Veteran


Jun 17, 2009, 11:16 AM

Post #15 of 16 (2040 views)
Re: [bclark8923] Teaching myself some basic perl [In reply to] Can't Post

I give you credit for being quick, but the code is not very good perl code.

Hint.... whenever you hear "unique" in perl it almost always means you will use a hash.
-------------------------------------------------


(This post was edited by KevinR on Jun 17, 2009, 11:18 AM)


bclark8923
Novice

Jun 17, 2009, 11:18 AM

Post #16 of 16 (2036 views)
Re: [KevinR] Teaching myself some basic perl [In reply to] Can't Post

yeah im still in a C++ mindset, learnin all the tricks of perl n stuff, brb lookin up perl hashes

and by C++ mindset i mean i learned hash maps in C++ recently still gotta get used to hashing everything, much more efficient


(This post was edited by bclark8923 on Jun 17, 2009, 11:24 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives