http://www.perlmonks.org?node_id=1004621


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!