Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,072 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,617 people online right now. Registration is fast and FREE... Join Now!




new to perl

 
Reply to this topicStart new topic

new to perl, programming

sniper24
3 Oct, 2008 - 11:25 PM
Post #1

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 2

hello i have a question im using python 2.5 lets say i wanna build a calculator how u go by doing that? does class come first or function im new to programming and is quit diffacult to understand can u show step by step in creating a simple caculator and see if i could get the hand of it thanks it would be well appreciated smile.gif
User is offlineProfile CardPM
+Quote Post

usamamuneeb
RE: New To Perl
4 Oct, 2008 - 01:29 AM
Post #2

D.I.C Head
**

Joined: 4 Sep, 2008
Posts: 67


My Contributions
Here's a simple calculator:

CODE
#!/usr/bin/perl
$nbArguments = $#ARGV + 1;
print "number of arguments: $nbArguments\n";
exit(1) unless $nbArguments == 3;
$a = $ARGV[0];
$b = $ARGV[2];
$operation = $ARGV[1];
if ($operation eq "+") {
     $result = $a + $b;
}
elsif ($operation eq "-") {
     $result = $a - $b;
}
elsif ($operation eq "/") {
     $result = $a / $b;
}
elsif ($operation eq "x") {
     $result = $a * $b;
}
print "$a $operation $b = $result\n";


The first line tells where is the perl binary. The second line gets the number of arguements (must be three, two numbers and an operator). Because the count starts from zero, one is added. the third line shows the num of args. 1 is a unix convention of error occurance. so the prog will exit unless there are three arguements. The next three subsequent lines store the arguements in an array. $ARGV[0] and $ARGV[2] store the numbers (operands) and are assigned $a and $b instances respectively while the $ARGV[1] stores the operator. Then there are if and elsif statements. The perform the action according to the operator. and store value in result. The last line prints the expression provided by the user and the result and \n is a newline.

This post has been edited by usamamuneeb: 4 Oct, 2008 - 01:34 AM
User is offlineProfile CardPM
+Quote Post

sniper24
RE: New To Perl
4 Oct, 2008 - 03:53 PM
Post #3

New D.I.C Head
*

Joined: 3 Oct, 2008
Posts: 2

QUOTE(usamamuneeb @ 4 Oct, 2008 - 02:29 AM) *

Here's a simple calculator:

CODE
#!/usr/bin/perl
$nbArguments = $#ARGV + 1;
print "number of arguments: $nbArguments\n";
exit(1) unless $nbArguments == 3;
$a = $ARGV[0];
$b = $ARGV[2];
$operation = $ARGV[1];
if ($operation eq "+") {
     $result = $a + $b;
}
elsif ($operation eq "-") {
     $result = $a - $b;
}
elsif ($operation eq "/") {
     $result = $a / $b;
}
elsif ($operation eq "x") {
     $result = $a * $b;
}
print "$a $operation $b = $result\n";


The first line tells where is the perl binary. The second line gets the number of arguements (must be three, two numbers and an operator). Because the count starts from zero, one is added. the third line shows the num of args. 1 is a unix convention of error occurance. so the prog will exit unless there are three arguements. The next three subsequent lines store the arguements in an array. $ARGV[0] and $ARGV[2] store the numbers (operands) and are assigned $a and $b instances respectively while the $ARGV[1] stores the operator. Then there are if and elsif statements. The perform the action according to the operator. and store value in result. The last line prints the expression provided by the user and the result and \n is a newline.


im sorry about that it wasnt new to perl i meant to say new to python

thats a perl programming i asked about python how about doing a python program like building a calulator with python

This post has been edited by sniper24: 4 Oct, 2008 - 03:49 PM
User is offlineProfile CardPM
+Quote Post

Moonbat
RE: New To Perl
4 Oct, 2008 - 05:00 PM
Post #4

D.I.C Regular
Group Icon

Joined: 30 Jun, 2008
Posts: 391



Thanked: 22 times
Dream Kudos: 600
My Contributions
Not to sound rude, but why is this in the PHP section?
User is online!Profile CardPM
+Quote Post

girasquid
RE: New To Perl
4 Oct, 2008 - 05:28 PM
Post #5

Barbarbar
Group Icon

Joined: 3 Oct, 2006
Posts: 1,266



Thanked: 14 times
Dream Kudos: 650
My Contributions
Try reading through this book to get the hang of python.
User is offlineProfile CardPM
+Quote Post

akozlik
RE: New To Perl
6 Oct, 2008 - 06:12 AM
Post #6

D.I.C Addict
Group Icon

Joined: 25 Feb, 2008
Posts: 603



Thanked: 23 times
Dream Kudos: 750
My Contributions
Next time post these questions in the Perl and Python forum.

Hopefully somebody will move it for you where it can get the attention it needs.
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: New To Perl
6 Oct, 2008 - 08:01 AM
Post #7

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 3,984



Thanked: 16 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
**Moved to Perl and Python **

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 07:10PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month