in reply to Bug in code
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
|
|---|