in reply to Update: problem with scalar
It doesn't matter to me what the input looks like or where it comes from. When I expect it to be of a certain type, I test to see that it really is that type before I actually use it (i.e. I seldom treat data files or STDIN as "trusted" sources).use Scalar::Util 'looks_like_number'; my @array; while (<>) { chomp; if ( looks_like_number( $_ )) { push @array, $_; } else { # skip this part if you don't care to know about bad input +... warn "Unusable input line skipped: $_\n"; } }
UPDATE: Having played with that snippet a bit, I'm happy to notice that leading and trailing whitespace characters do not affect the results of "looks_like_number()" (i.e. it returns true for " 123 ", "\t-456\n", and so on). The same is true for arithmetic operators: you can add, subtract, multiply and divide numeric strings that have leading and trailing whitespace.
|
|---|