Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Please Help! This is a class assignment given to me.

by harangzsolt33 (Chaplain)
on Dec 07, 2019 at 06:11 UTC ( [id://11109790]=note: print w/replies, xml ) Need Help??


in reply to Please Help! This is a class assignment given to me.

I dont know where to start or how to start

You should start with the math. Figure out what you want the program to calculate and how to calculate it. Once you get that straight, writing the perl script will be very easy.

You want to find the profit. And you have 10 pizzas. Let's find the profit for one pizza first and then we multiply it by 10. Okay?

The profit equals income - expense. Income is the price you get for the pizza, and expense is what you pay for the ingredients and cooking and labor, etc.

In this case, figuring out the expense is easy, because we are told that the pizza price is 250. So, 250 includes all the expenses. The income is 300, because that's what you're selling it for. And the difference is 50. That's your profit. That's the question.

But then what if you sell the pizza 10% cheaper? Now, instead of selling it for 300, you sell it for 270, because that's 10% less. Now, if you pay 250 for the pizza and sell it for 270, then you still have a profit of 20 on each pizza.

So, the math is pretty simple... Yes, writing the perl code is also simple, but you have to figure it out yourself.

Every perl program starts with the same three lines of code. And this is just to get you started:

#!/usr/bin/perl -w

use strict;
use warnings;

my $A = 250; # Pizza price
my $B = 300; # Selling price
my $C = $B - $A; # Difference
print "A = $A \n B = $B \n C = $C \n";

In programming languages, when you write A = 4, that's not a math equation. It's an instruction for the processor to store the value "4" in a memory location which we label as "A" If you want to do a multiplication, then you would do something like:

A = 4 * 12;

But in perl, every label or variable I should say is preceded by a $ sign. And you should declare the variable before you use it. That means the first time you assign a value to it, you should put the word "my" in front of it:

my $A = 4;

If you want to do multiplication in perl, you would do:

my $A = 4 * 12;
$A = $A * 2;

When you multiply a value by something else, you can write it as :

my $A = 2;
$A *= 5;
print $A; # Output: 10

Hope that wasn't too confusing.

  • Comment on Re: Please Help! This is a class assignment given to me.

Replies are listed 'Best First'.
Re^2: Please Help! This is a class assignment given to me.
by haukex (Archbishop) on Dec 07, 2019 at 10:01 UTC
    Every perl program starts with the same three lines of code. And this is just to get you started:
    #!/usr/bin/perl -w use strict; use warnings;

    Both use warnings and -w isn't needed, see: What's wrong with -w and $^W

    Personally I prefer the shebang #!/usr/bin/env perl to say "use whatever perl is in my PATH" (see env), it'll also work better with e.g. perlbrew. Pointing to a specific perl binary makes sense if distributing a script to a system where you may not have control over the PATH (e.g. CGI scripts on hosting providers).

    Update: I agree with Tux's reply: In the case of the shebang, TIMTOWTDI.

      Pointing to a specific perl binary makes sense if distributing a script to a system where you may not have control over the PATH.

      Or if you want to make sure it uses a specific (non system) perl install that has modules installed that are not available in general distributions or have libraries linked that are specific to the script or …

      Personally I always use #!/pro/bin/perl, which is where I have my production perl installed. If I do not need anything specific, and system-perl will do (which is seldom the case), I make /pro/bin/perl a symlink to the required perl. TIMTOWTDI


      Enjoy, Have FUN! H.Merijn
Re^2: Please Help! This is a class assignment given to me.
by hippo (Bishop) on Dec 07, 2019 at 11:37 UTC
    my $A = 250; # Pizza price my $B = 300; # Selling price my $C = $B - $A; # Difference print "A = $A \n B = $B \n C = $C \n";

    Good news - Perl actually allows variable names with more than one character in them. This means that you no longer have to remember what $B is supposed to represent. Instead you can give that variable a meaningful name.

    my $cost_price = 250; my $sale_price = 300; my $profit = $sale_price - $cost_price; print "Make them for $cost_price\nSell them for $sale_price\nMake $pro +fit on each\n";

    Addendum: There is, as with most things, a happy medium.

      You should consider to whom the names are 'meaningful'. I would consider the name '$selling_price' to be ambiguous (before or after the discount?), but the 'client' who wrote this spec clearly intends the before case. This name probably meets the 'happy medium' rule, but only if you mean exactly the same thing that your client does.
      Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11109790]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-19 03:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found