Beefy Boxes and Bandwidth Generously Provided by pair Networks Bob
Do you know where your variables are?
 
PerlMonks  

Bug in code

by disulfidebond (Novice)
on Nov 19, 2012 at 21:36 UTC ( #1004615=perlquestion: print w/ replies, xml ) Need Help??
disulfidebond has asked for the wisdom of the Perl Monks concerning the following question:

I wrote a script to track cell counting in my lab. It prompts the user to input the number of counts (I may do multiple at a time), then a subroutine prompts again to get each count and converts it into the total number of cells and returns it, where a final for loop displays all the elements of the array in a nice readable format. Only problem is it doesn't work. It either 1) halts arbitrarily, 2) prompts for the wrong number of counts, or 3) works correctly. I've verified the array is initialized and assigned values, and all variables are assigned values from user input or the subroutine, but I can't find where the bug is. It's probably something very simple, can someone please help?
#!/usr/bin/perl -w use bignum; # 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"; # } print "Enter number of counts.\n->"; my $ct = <>; chomp $ct; print "\n$ct"; @arr = (); for ($i = 0; $i < $ct; $i++) { my $count_i = &indiv_count(); push (@arr, $count_i); } $arr_len = @arr; print "The counts were:\n"; for ($j = 0; $j< $arr_len; $j++) { print "Count number $j was $arr[$j]\n"; } sub indiv_count { my ($input); print "Enter individual count from scope. Press Enter without inp +ut to end list\n"; @scope_ct = (); while ($input = <STDIN>) { chomp $input; last if ($input =~ /^\s*$/); push (@scope_ct, $input); } my $scope_ct = @scope_ct; for ($i=0; $i<$scope_ct; $i++){ $cell_sum += pop(@scope_ct); } $cell_ct = (($cell_sum/$scope_ct)/0.0000015); return $cell_ct; }

Comment on Bug in code
Download Code
Re: Bug in code
by frozenwithjoy (Curate) on Nov 19, 2012 at 22:18 UTC
    My first recommendation is that you need use strict; in your code. This will REALLY help with the debugging.

    Second: don't use the same name for scalars and arrays (e.g., my $scope_ct = @scope_ct;). It is too easy to make a mistake w/o realizing it.

    EDIT: I made a few very minor changes based on the errors from using strict/warnings and now your script seems to work (see below). If I were you, I'd still go back and rename some variables to make your script easier to expand/maintain/troubleshoot in the future.

    #!/usr/bin/env perl use strict; use warnings; use bignum; # 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"; # } print "Enter number of counts.\n->"; my $ct = <>; chomp $ct; print "\n$ct"; my @arr = (); for ( my $i = 0 ; $i < $ct ; $i++ ) { my $count_i = &indiv_count(); push( @arr, $count_i ); } my $arr_len = @arr; print "The counts were:\n"; for ( my $j = 0 ; $j < $arr_len ; $j++ ) { my $iteration = $j + 1; print "Count number $iteration was $arr[$j]\n"; } sub indiv_count { my ($input); print "Enter individual count from scope. Press Enter without inp +ut to end list\n"; my @scope_ct = (); while ( $input = <STDIN> ) { chomp $input; last if ( $input =~ /^\s*$/ ); push( @scope_ct, $input ); } my $cell_sum; my $scope_ct = @scope_ct; for ( my $i = 0 ; $i < $scope_ct ; $i++ ) { $cell_sum += pop(@scope_ct); } my $cell_ct = ( ( $cell_sum / $scope_ct ) / 0.0000015 ); return $cell_ct; } __END__ $ ./cell_counter.pl Enter number of counts. ->3 3Enter individual count from scope. Press Enter without input to end +list 1 2 3 Enter individual count from scope. Press Enter without input to end l +ist 4 5 6 Enter individual count from scope. Press Enter without input to end l +ist 7 8 9 10 The counts were: Count number 1 was 1333333.333333333333333333333333333333333 Count number 2 was 3333333.333333333333333333333333333333333 Count number 3 was 5666666.666666666666666666666666666666667
Re: Bug in code
by Kenosis (Priest) on Nov 19, 2012 at 22:31 UTC

    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!

Re: Bug in code
by linuxkid (Sexton) on Nov 20, 2012 at 01:57 UTC

    just a suggestion, use an en passant modification of your input, like chomp($input = <STDIN>);

    --linuxkid


    imrunningoutofideas.co.cc
Re: Bug in code
by graff (Chancellor) on Nov 20, 2012 at 03:48 UTC
    Where does the user get the values that are being typed in manually? Wouldn't it be easier and more reliable for the user to store these values in a simple little text file? It would be very easy to make your script read from a file instead of the keyboard. (If the user is reading these numbers from some other data file, it would be even better for the script to read that data file, instead of making the user type the values manually while your script is running.)

    Reading input from a file will make it a lot easier to debug (and easier to ask for help: show the input to the script, and tell us what you'd like to see as output for the given input). For testing purposes, you can even included test data as part of the source code file. Here's an example of a script that allows the user to name a file on the command line, but if no file name is given, it reads sample data from the source code file itself:

    #!/usr/bin/perl use strict; my $source = "test data"; if ( @ARGV ) { $source = shift; open( DATA, '<', $source ) or die "$source: $!\n Usage: $0 [data.file]\n"; } my @inputs; while (<DATA>) { chomp; push @inputs, $_; } # do some stuff with @inputs, as you see fit... e.g. my $sum; my $input_list; for my $value ( @inputs ) { $sum += $value if ( $value =~ /^-?[\d.]+$/ ); $input_list .= " $value"; } printf( "There were %d values in '$source': %s\n The numeric sum is %f +\n", scalar @inputs, $input_list, $sum ); __DATA__ 5 10 non_numeric 15 20

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-04-19 15:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls