Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Bug in code

by Kenosis (Priest)
on Nov 19, 2012 at 22:31 UTC ( #1004621=note: print w/ replies, xml ) Need Help??


in reply to Bug in code

I agree with frozenwithjoy that you should use strict;. Always do that and also use warnings;

I did encounter an error when running your code, so I made just a few changes. One is replacing the C-style for loops with something a bit more Perlish. Also used List::Util to sum @scope_ct:

#!/usr/bin/perl -w use strict; use warnings; use bignum; use List::Util qw/sum/; # pseudocode # print "Enter number of counts" # $ct = <>; # @arr = (); # for ($i = 0; $i < $ct; $i++) { # each iteration of loop calls subroutine # to get cell counts from user and stores in variable # push() variable to store in array # $count_i = &indiv_count() # push (@arr, $count_i); # } # output @arr # $arr_len = @arr; # print "The counts were:\n"; # for ($j = 0; $j< $arr_len; $j++) { # print "Count number $j was arr[$j]\n"; # } my @arr; print "Enter number of counts.\n->"; chomp( my $count = <> ); print "\n$count\n"; for ( 1 .. $count ) { print "Enter individual count from scope. Press Enter without input to end +list.\n"; my $count_i = indiv_count(); push @arr, $count_i; } print "The counts were:\n"; print "Count number $_ was $arr[$_-1]\n" for 1 .. @arr; sub indiv_count { my @scope_ct; while ( chomp( my $input = <> ) ) { last if $input =~ /^\s*$/; push @scope_ct, $input; } my $scope_ct = @scope_ct; my $cell_sum = sum @scope_ct; my $cell_ct = ( $cell_sum / $scope_ct ) / 0.0000015; return $cell_ct; }

Didn't encounter any more errors, but I may not have sufficiently tested it.

Hope this helps!


Comment on Re: Bug in code
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2015-07-06 08:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (70 votes), past polls