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

In reply to Re: Bug in code by graff
in thread Bug in code by disulfidebond

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.