Replace a line

V

Ved

Hi all,
I am learning perl and could have done this by myself by taking a few
days. But its needed early.

I have a file which has major chunk contaning samething like show
below:

if ( dat0s0 > dat1s0) then
dat0s1 <= dat0s0 ;
dat1s1 <= dat1s0 ;
else
dat0s1 <= dat1s0 ;
dat1s1 <= dat0s0 ;
end if;

if ( dat2s0 > dat3s0) then
dat2s1 <= dat2s0 ;
dat3s1 <= dat3s0 ;
else
dat2s1 <= dat3s0;
dat3s1 <= dat2s0 ;
end if;

----
if ( dat4s0 > dat5s0) then

dat4s1 <= dat4s0 ;
dat5s1 <= dat5s0 ;
else
dat4s1 <= dat5s0;
dat5s1 <= dat4s0 ;
end if;
..........
..............
.......... and so on ........

I have to replace only the lines

if ( dat0s0 > dat1s0) then

with

if ( abs(dat0s0) > abs(dat1s0)) then

Rest of the code has to be as it is.

Please help
Thanks
Ved
 
K

Klaus

Ved said:
Hi all,
I am learning perl and could have done this by myself by taking a few
days. But its needed early.

There are, obviously, different opinions, but in my view, the quickest
way to learn Perl is by reading the reading books. Have a look at
"perldoc -q books".
 
T

Tad McClellan

Ved said:
I have to replace only the lines

if ( dat0s0 > dat1s0) then

with

if ( abs(dat0s0) > abs(dat1s0)) then


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

while ( <DATA> ) {
if ( /^\s*if.*then$/ ) {
s/(\S+)(\s*\))/abs($1)$2/; # 2nd operand
s/(\(\s*)(\S+)/$1abs($2)/; # 1st operand
}
print;
}

__DATA__
if ( dat0s0 > dat1s0) then
dat0s1 <= dat0s0 ;
dat1s1 <= dat1s0 ;
else
dat0s1 <= dat1s0 ;
dat1s1 <= dat0s0 ;
end if;

if ( dat2s0 > dat3s0) then
dat2s1 <= dat2s0 ;
dat3s1 <= dat3s0 ;
else
dat2s1 <= dat3s0;
dat3s1 <= dat2s0 ;
end if;

----
if ( dat4s0 > dat5s0) then

dat4s1 <= dat4s0 ;
dat5s1 <= dat5s0 ;
else
dat4s1 <= dat5s0;
dat5s1 <= dat4s0 ;
end if;
 
V

Ved

Hi Ted,
When I run this program, it printed what is written in _DATA_
Is the code complete?

Regards
Ved
 
A

anno4000

Please don't top-post. I have moved your reply in context.
Hi Ted,
When I run this program, it printed what is written in _DATA_
Is the code complete?

His name is Tad.

The program adds the required abs() to the operands. Please look again.

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top