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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

variable values

Status
Not open for further replies.

je150

IS-IT--Management
Jun 10, 2003
33
0
0
US
hello, i am working on a very large script that takes information from a .txt document and parses it, places it in email messages and sends them. EVERYTHING works, except my addition script. for some reason a single "," (comma) in a price throws it off. for example the data is $1,234.34 and is added to several other values, however, the total is displayed as a value way below 1,000. its closer to 298.99.
here is the portion of the script that adds the values together, they are placed in the variable $mysubtotal as pure numbers with decimals and the occasional comma. the commas seem to be throwing this off, is there a way to remove them before it gets added.

$mysubtotal = substr($total{$key}, 1);
print "$mysubtotal\n";
$fulltotal += $mysubtotal;


thanks
 
$mysubtotal=~ s/\,//g; dont know if you need to escape, if that doesn't work
try $mysubtotal =~ s/,//g;

HTH
--Paul
 
If I understand you correctly, you will need to strip the comma out before storing it in the associative array. For example the following code:

#!/usr/bin/perl -w
%total=("apples", 123.45, "bananas", 2345.00, "oranges", 2,500);
foreach $fruit (sort keys(%total)) {
print ("$fruit: $total{$fruit}\n");
}

produces the following output:

apples: 123.45
bananas: 2345
oranges: 2

Notice how the 2,500 becomes a 2.
If this is what is happening in your code here is an example of what to do to fix it:

#!/usr/bin/perl -w

$inputline = <STDIN>; #Input from keyboard
$inputline =~ s/\,//g; #This removes the comma
%total = split(/\s+/, $inputline);
#%total=(&quot;apples&quot;, 123.45, &quot;bananas&quot;, 2345.00, &quot;oranges&quot;, 2,500);
$mysubtotal =0;
$fulltotal =0;
foreach $fruit (sort keys(%total)) {
print (&quot;$fruit: $total{$fruit}\n&quot;);
$mysubtotal = $total{$fruit};
$fulltotal += $mysubtotal;
}
print &quot;TOTAL: $fulltotal&quot;;

Keyboard input:
apples 5.45 bananas 2345.00 oranges 2521.98

Result:
apples: 5.45
bananas: 2345.00
oranges: 2521.98
TOTAL: 4872.43
 
If I understand you correctly, you will need to strip the comma out before storing it in the associative array. For example the following code:

#!/usr/bin/perl -w
%total=(&quot;apples&quot;, 123.45, &quot;bananas&quot;, 2345.00, &quot;oranges&quot;, 2,500);
foreach $fruit (sort keys(%total)) {
print (&quot;$fruit: $total{$fruit}\n&quot;);
}

produces the following output:

apples: 123.45
bananas: 2345
oranges: 2

Notice how the 2,500 becomes a 2.
If this is what is happening in your code here is an example of what to do to fix it:

#!/usr/bin/perl -w

$inputline = <STDIN>; #Input from keyboard
$inputline =~ s/\,//g; #This removes the comma
%total = split(/\s+/, $inputline);
#%total=(&quot;apples&quot;, 123.45, &quot;bananas&quot;, 2345.00, &quot;oranges&quot;, 2,500);
$mysubtotal =0;
$fulltotal =0;
foreach $fruit (sort keys(%total)) {
print (&quot;$fruit: $total{$fruit}\n&quot;);
$mysubtotal = $total{$fruit};
$fulltotal += $mysubtotal;
}
print &quot;TOTAL: $fulltotal&quot;;

Keyboard input:
apples 5.45 bananas 2345.00 oranges 2521.98

Result:
apples: 5.45
bananas: 2345.00
oranges: 2521.98
TOTAL: 4872.43

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top