What's going wrong in your code is that $1 contains the string "0xdf7780", which Perl does not automatically convert to a number. You can use the hex function to convert it to a number first, and then use sprintf to convert the output back to hex:
$line1 =~ /addr:([a-z0-9-]+)\s+/; my $one = $num + hex $1; $one = sprintf "0x%x", $one; $line1 =~ s/addr\:0x.*qospri/addr\:$one qospri/;
However, it's also possible to do it all in one statement, using the special s///e, which runs the replacement part as Perl code and inserts its return value as the replacement, so you can use it to run sprintf directly. I've also used \K and a (?=...) lookahead to exclude those strings from the match, so they're not changed by the replacement.
$line1 =~ s{ addr:0x \K ([0-9a-fA-F]*) (?=\s+qospri) } { sprintf "%x", hex($1)+$num }xe;
By the way, you should always Use strict and warnings, and use proper indentation to make your code easier to read (perltidy can help with that).
Update: I added the /x modifier to the regex to make it easier to read. Also switched from (?=\ qospri) to (?=\s+qospri) to make it equivalent to the original regex.
In reply to Re: Performing addition on hex value extracted from a string
by haukex
in thread Performing addition on hex value extracted from a string
by syedasadali95
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |