Re: Arithmetic operation inside substitution



"Amit Koren" schreef:

$string="blah 10 20";
I need to add 5 to the 10, and add 7 to the 20.


#!/usr/bin/perl
use strict;
use warnings;

my %add = (
10 => 5,
20 => 7,
);

while ( <DATA> ) {
s/ ( [0-9]+ ) / exists($add{$1}) ? $add{$1} : $1 /xge;
print;
}

__DATA__
blah 10 37 20 blah




--
Affijn, Ruud

"Gewoon is een tijger."
.



Relevant Pages

  • scope and linkage
    ... I'm working on a disassembler for a school project. ... Things work but I get compiler warnings as follows: ... defs.h:44: warning: 'blah' defined but not used ... The warnings are invisible w/o the -Wall compile flag. ...
    (comp.lang.c)
  • Re: scope and linkage
    ... Things work but I get compiler warnings as follows: ... defs.h:44: warning: 'blah' defined but not used ... The warnings are invisible w/o the -Wall compile flag. ... Boeing Associate Technical Fellow ...
    (comp.lang.c)
  • Re: Printing text between two blank lines
    ... Aren't you using strictures and warnings?? ... I'd use a foreach loop instead of map in void context. ... strict and warnings, ... blah blah weather, sports ...
    (perl.beginners)
  • combine 2 "open for reading" filehandles
    ... use strict; ... use warnings; ... oops again ...
    (perl.beginners)
  • Re: date format search insdie the files
    ... use strict; ... use warnings; ... "Gewoon is een tijger." ...
    (perl.beginners)