Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variable type or conversion or something

Status
Not open for further replies.

HaroldMcF

MIS
Jul 29, 2002
8
US
I'm pulling information from a text file into an array and that works well. The problem is that I need to add two of those items together. It appears that it is treating them as strings or something because it will not add them together. Instead of returning a result it prints a zero.

Is there something to convert text to integer or something like that? Even if it converts it to a number so I can add them and then returns it to the array as a string that is fine, but I need to be able to add them somehow.
 
Perl does all of that automatically depending on context, so it should work without any effort. Can you post the problematic code? ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Here is some of the relevant code:
The array @thirdpass contains each line of an input file where the fields are separated by a caret.
So, I bring each line in and parse based on the caret.
I then have some nested "if" statements to determine what value should be assigned to a particular element in the array @acctset. The result that I am seeking is that sometimes there are several lines of input that have the same account number. When that is the case I need to add the values.
So, what I tried was changing the line "$acctset[2] = $pd2;" to "$acctset[2] = $acctset[2] + $pd2". This returned a zero. Originally it returned the correct value.


foreach $inline (@thirdpass)
{
($accnoin, $acdescin, $yearin, $fdesigin, $begbalin, $pd1, $pd2, $pd3, $pd4, $pd5, $pd6, $pd7, $pd8, $pd9, $pd10, $pd11, $pd12) = split(/\^/, $inline);


if($acctno1 eq $accnoin) #Check to see if account no is the same as prior line
{
if($col2yr eq $yearin && $col2des eq $fdesigin)
{
if($col2pd eq "pd1")
{
$acctset[2] = $pd1;
}
if($col2pd eq "pd2")
{
$acctset[2] = $pd2;
}
.
.
.

Hope that helps in figuring out the problem.
 
It might be a problem with the array element being defined the first time in (but it shouldn't, I think undef in numeric context returns zero). Might try something like this:
Code:
$acctset[2] = (defined($acctset[2])) ? ($acctset[2] + $pd2) : ($pd2);
It tests if it's defined, and if it is, adds it to what's there, and if not, just sets it to $pd2. ----------------------------------------------------------------------------------
...but I'm just a C man trying to see the light
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top