Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Diagnose code

by stamp1982 (Novice)
on Jun 27, 2013 at 01:09 UTC ( #1040884=perlquestion: print w/replies, xml ) Need Help??
stamp1982 has asked for the wisdom of the Perl Monks concerning the following question:

I have this question and below it is what I have now it does not seem to work. can we troubleshoot to gether?

Ask the user for a list of sequence lengths, separated by whitespace (Example: 100 123 45 …etc.). The sequence lengths will be stored in a string called $input. a. Split the String $input and create an array. b. Use the foreach loop to get the sum of all sequence lengths. c. Print the average. this is what I have now: but I don't get the space at the print command.

use strict; use warnings; my $i = 0; my $input = ""; while ($i < 3) { chomp(my $num = <>); $input .= $num . "". $num; $i++; } print "Total input is $input\n"; my @nums = split(' ', $input); foreach (@nums) { my $sum += $_; } print my $sum / @nums . "\n"; exit;

Replies are listed 'Best First'.
Re: Diagnose code
by Athanasius (Chancellor) on Jun 27, 2013 at 02:13 UTC

    Hello stamp1982,

    You should provide a prompt to alert the user that input is required. For example: print "Enter a list of sequence lengths, separated by whitespace:\n";. And this line: $input .= $num . "". $num; is wrong; it should be something like $input .= $num . " ";. Also, to split on whitespace, you should use split(/\s+/, $input);. See split.

    But the main problem arises from your repeated declarations of the $sum variable. my is used to declare and initialise a lexical variable (and if no initialisation is given explicitly, a scalar variable is by default initialised to undef). So the final few lines should be:

    print "Total input is: $input\n"; my @nums = split(/\s+/, $input); my $sum; $sum += $_ for @nums; print $sum / @nums . "\n";

    Note that $sum is here declared (and initialised) once only. Any further use of my on $sum would create a new variable with the same name, which is not what you want. See e.g. Variable scoping.

    Hope that helps,

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

Re: Diagnose code
by Loops (Curate) on Jun 27, 2013 at 02:17 UTC

    Only a couple small points. The corrected code follows

    The .= operator appends anything on the righthand side to the left.. So you use either x = x . y; or x .= y; but not x .= x . y;

    In order for the calculated sum to be available outside of the foreach loop, it must be declared outside of the foreach loop. This also means that it can't be redeclared with "my" inside the foreach loop or in the ensuing print statement.

    use strict; use warnings; my $i = 0; my $input = ""; while ($i < 3) { chomp(my $num = <>); $input .= " $num"; $i++; } print "Total input is $input\n"; my @nums = split(' ', $input); my $sum; foreach (@nums) { $sum += $_; } print $sum / @nums . "\n"; exit;

    There are many other ways this code can be improved. Hopefully the above points get you going again though.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1040884]
Approved by Athanasius
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (7)
As of 2018-06-21 01:21 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.