Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

replace a character based on rule file

by lakssreedhar (Acolyte)
on Dec 04, 2012 at 06:47 UTC ( [id://1007016]=perlquestion: print w/replies, xml ) Need Help??

lakssreedhar has asked for the wisdom of the Perl Monks concerning the following question:

i have a file with words door,name,fame,dad,naming and so on.I have a rule file that whenever 'na' is followed by letter m then the letter m should be replaced by letter d and the whole word should be as na+d.so the output should be door,na+de,fame,dad,na+ding.The code below doesnt seems to work.

while(<>) { chomp; $inp_word4 = $_; @inp_word=split(//,$inp_word4); for($i=0;$i<@inp_word;$i++) { if(($inp_word[$i]=~/na/)&&($inp_word[++$i]=~/m/)) { $inp_word[++$i]=~s/m/d/; } print "$inp_word"; } }

Replies are listed 'Best First'.
Re: replace a character based on rule file
by kcott (Archbishop) on Dec 04, 2012 at 08:17 UTC
Re: replace a character based on rule file
by karlgoethebier (Abbot) on Dec 04, 2012 at 07:59 UTC

    "I have a file with words...I have a rule file... should be..."

    OK, but was is the question? Doesn't it work? It isn't...? You don't know how to do it? Something else?

    Regards, Karl

    Update: Some inspiration can be found here: How do I post a question effectively?

    «The Crux of the Biscuit is the Apostrophe»

Re: replace a character based on rule file
by graff (Chancellor) on Dec 05, 2012 at 02:41 UTC
    The reason the OP code doesn't work is that the @inp_word array contains elements of one character each, because it gets the output of split(//,$inp_word4); But then your logic depends on some element of @input_word containing "na", which can never happen.

    If I understand your task description correctly, I think you just need a basic regex substitution:

    while(<>) { s/(?<=na)m/+d/g; print; }
    The (?<=na) part there is called a "positive look-back assertion" - check that out on perlre. It says that any "m" will be replaced by "+d" if it's preceded by "na".
Re: replace a character based on rule file
by nithins (Sexton) on Dec 04, 2012 at 13:24 UTC
    while(<FH>) { my $inp_word4 = $_; my $data = 'undef'; @inp_word=split(//,$inp_word4); for(my $i=0;$i<@inp_word;$i++) { if (($inp_word[$i]=~m/(n)/i) && ($data eq 'undef') ){ $data =$1; next; } if (($inp_word[$i] =~ m/(a)/i) && ( $data eq 'n')){ $data = $data.$1; next; } if (($inp_word[$i] =~ m/(m)/i) && ( $data eq 'na')){ $data = 'undef'; $inp_word[$i] = '+d'; next; } } print @inp_word ; }

    i have tested for the test cases "name domr fame naming namnam nanaming " and it's working.you can still make the code smaller.

    in your code the if statement can never become true , your trying to match single character say 'n' with 'na'

Re: replace a character based on rule file
by karlgoethebier (Abbot) on Dec 04, 2012 at 08:57 UTC

    TWTOWTDI:

    «The Crux of the Biscuit is the Apostrophe»

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1007016]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-03-29 07:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found