perl - Calculate difference of two values in hash -


input file

id      s1              s2              s3             s4     r1  r2          r3  r4          r5  r6         r7  r8 .   .       .   .   .       . .       .       .       .       . 

a id may string
r1,r2,r3,r4.....are numeric values

i have use perl script.i have read above data file , read in hashes.i have calculate difference of r1 , r2 if difference greater 0 print r1 , r2 is,other put 0 in place of r1 , r2. next find difference of r3 , r4. how can above problem using hashes

output of perl program:

id      s1             s2           s3              s4     r1  r2          0  0         0  0           r7  r8 .   .       .   .   .       . .       .       .       .       . .   .       .       .       .    

why need hash find difference of pairs of numbers???

print(scalar(<>));  while (<>) {    @parts = split ' ';     (my $i=1; $i<@parts; $i+=2) {       if ($parts[$i+0] == $parts[$i+1]) {          $parts[$i+0] = '0';          $parts[$i+1] = '0';       }    }     print(join(' ', @parts), "\n"); } 

or if want preserve formatting:

print(scalar(<>));  while (<>) {    chomp;    @parts = split /(\s+)/;     (my $i=2; $i<@parts; $i+=4) {       if ($parts[$i+0] == $parts[$i+2]) {          $parts[$i+0] = sprintf('%*s',  length($parts[$i+0]), '0');          $parts[$i+2] = sprintf('%*s', -length($parts[$i+2]), '0');       }    }     print(join('', @parts), "\n"); } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -