We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,440 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

First occurrence of PP: tag to HL: tag

Sample content of the file

PP: Happy Sunday!
PP: It's a good weather.
PP: Have a blessed Sunday everyone.

the output should be

HL: Happy Sunday!
PP: It's a good weather.
PP: Have a blessed Sunday everyone

My code to generate that

$length=@fileinput;
	 if($line=~/PP:\s/)
	 { {if($j<$length)
	    {if($line[$j-1]!~/PP:\s/ && $line[$j-1]!~/HL:\s/) 
	     {		 
		    $line=~s/$`/HL: /;
	        $j++;
	     }
	    }
       }
     }

but the output of this is it all replace PP: to HL:, how can I do the first occurrence of PP: should only be replace? thank you for the help.

2
Contributors
1
Reply
18 Hours
Discussion Span
2 Years Ago
Last Updated
2
Views
perlnoob
Newbie Poster
8 posts since Mar 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
#!/usr/bin/perl
use strict;
use warnings;

my $pp_replaced = 0;#Indicates no occurrence of PP has been replaced yet
while(<DATA>){
    if (m/^PP:/ and $pp_replaced == 0){
        s/PP:/HL:/;
        $pp_replaced++;#Add 1
    }
    print;
}
__DATA__
PP: Happy Sunday!
PP: It's a good weather.
PP: Have a blessed Sunday everyone.
d5e5
Practically a Posting Shark
831 posts since Sep 2009
Reputation Points: 162
Solved Threads: 163
Skill Endorsements: 1

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0659 seconds using 2.75MB