Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

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!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1004621]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2016-12-05 04:26 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (72 votes). Check out past polls.