Home: Perl Programming Help: Intermediate:
after substituting given word with respected value i want to do math operation.



harshmane
Novice

Jul 17, 2011, 11:54 PM


Views: 1760
after substituting given word with respected value i want to do math operation.

after substituting given word with respected value i want to do math operation.


like i open 1 file in starting of the file i m having all d word with respected value wich store it in hash and i replace it also now i want to do some mathematical operzation on the same line wheere substitution happen like


Code
 hi=3 ; 
my=4;
before substitution.
hi + my
afetr substitution
3 +4.
now i want
7



BillKSmith
Veteran

Jul 18, 2011, 12:43 PM


Views: 1734
Re: [harshmane] after substituting given word with respected value i want to do math operation.


Code
use strict; 
use warnings;
my $string = 'hi + my';
my $hi = 3;
my $my = 4;
print $string, "\n";
$string =~ s/hi/$hi/;
$string =~ s/my/$my/;
print $string, "\n";
my $sum = eval($string);
print $sum, "\n";

Good Luck,
Bill