I'm in the process of moving a bunch of Perl scripts from an old WinNT server to a new Unix server. Most of the moving has gone without any problems, Perl is pretty cross-platform compatible. However, certain scripts are giving me trouble.
For example, one script reads numbers from a "|" delimited flat file, then adds up some of the numbers. The data is typically read in as strings and is forced into a number context. Problem is that
some strings that apparently have some kind of a leading space character " " in front of the number (ex. " 7") are read as having a numerical value of 0. So when I add this string to a $total, the $total does not change. This strangely occurs
only on the Unix box and with only
some numbers that have leading spaces, not
all. I found a way around the problem - substitute out all the space characters before adding. But, I'm still curious if anyone knows what's going on here... just in case this problem surfaces again in some other place on some other script. Here's a lil' bit o' code:
# ex. one database file -
if (-e $database) {
open(DB, $database) or die "Error opening file: $!\n";
while(<DB>){
push @results, $_;
}
close (DB);
foreach $entry (@results){
# at this pt. $p_data[3] may look like " 7"
my ($key, @p_data) = split(/\|/,$entry);
# $p_data[3] sometimes added to $total w/ numerical value of 0
$total += $p_data[3];
}
# do some other stuff with $total... blah, blah, blah
}
Thanks,
Carl