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
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.