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

output difficult to understand

by sonia (Initiate)
on Feb 11, 2014 at 10:53 UTC ( [id://1074382]=perlquestion: print w/replies, xml ) Need Help??

sonia has asked for the wisdom of the Perl Monks concerning the following question:

sir, A simple program of perl for caculating percentage of marks is giving characters in the output that are difficult to understand. program is
print(qq(enter marks in maths), \n); $math=<STDIN>; print(qq(enter marks in science),\n); $sci=<STDIN>; print(qq(enter marks in history),\n); $hist=<STDIN>; print(qq(enter marks in geography),\n); $geo=<STDIN>; $sum=($math+$sci+$hist+$geo); $per=($sum/400); $percentage=($per*100); print(qq(your percentage is=), $percentage);
</c> output shows following
<enter marks in mathsSCALAR<0X124DA44>>
the unwanted characters SCALAR<0X124DA44> are repeated for every entry

Replies are listed 'Best First'.
Re: output difficult to understand
by Athanasius (Archbishop) on Feb 11, 2014 at 13:04 UTC

    Hello sonia, and welcome to the Monastery!

    As parv says, the strange output occurs because the newline characters are unquoted. Here is some valuable advice I hope you will take to heart: Give yourself a safety net by starting each script with:

    use strict; use warnings;

    For your script in its present form, this will result in 18 errors. The first 14 of these are easily fixed by declaring each variable with my the first time it appears:

    ... my $math=<STDIN>; ... my $sci=<STDIN>; ...

    The last 4 errors are of the form:

    Bareword "n" not allowed while "strict subs" in use at ...

    which draws attention to the fact that the newline character \n is not quoted — hence, it’s a “bareword.”

    In the hope that you may find it helpful, here is how I would approach this task:

    use strict; use warnings; my @subjects = qw( maths science history geography ); my $sum = 0; foreach my $subject (@subjects) { print "Enter percentage mark in $subject:\n"; $sum += <STDIN>; } printf "Your average is: %.2f%%\n", ($sum / scalar(@subjects));

    The main advantages of this approach are:

    1. It avoids repeating the same code over and over.
    2. If a new subject is added, only one line (the one beginning my @subjects = ...) needs to be changed; the rest of the script will still work correctly.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: output difficult to understand
by parv (Parson) on Feb 11, 2014 at 11:01 UTC

    First of all, there are multiple gender types here, not just males. Second, add <c>...</c> (code tags) around your example program so that it would be easier to read.

    Try again by quoting the newline character, qq[\n], in your print statements.

      It looks like there are tags, but the first one is typoed...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (7)
As of 2024-04-19 09:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found