Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help for newbie substituting 1

Status
Not open for further replies.

dbrazeau

Programmer
Sep 5, 2007
2
US
I am tryig to substitute several things in a file going line by line. I was wondering if there is any way I can add a stop condition to to the substitution operator "s/PATERN/REPACMENT/g", because I do not want it to replace anything on the line after a ";" is reached. Basicly I'm writing a script to replace strings in a ASM file and I want it to ignore anything after the comment char ";".

I was just wondering if there is an easyer way rather than do this:

if(m/;/)
{$comntpos = pos(':')}

then checking every substitution if it happens after $comntpos.
if so ignore if not do the substitution.

Also as a side note if I wanted to set a $Var equal to a semicolen (i.e. $Var = ';';) is this how I do it??

 
You could break the line down first, then only perform your substitution on the part before the semi-colon ...
Code:
while ( <FILE> ) {
	my $line = $_;
	my @parts = split( /;/, $line, 2 );
	$parts[0] =~ s/PATTERN/REPLACEMENT/g;
	my $newline = join( ';', @parts );
}

--
-- Ghodmode

Give a man a fish and he'll come back to buy more... Teach a man to fish and you're out of business.
 
I assume your ASM files (that I don't know) can't contain any semicolons before the comment, also within delimited strings or escaped sequences, otherwise things start to get difficult.
Besides the solution by Ghodmode (likely the best one for long lines and/or many occurrences per line) you can try this one (assuming your line is in [tt]$_[/tt])
Code:
while(s/PATTERN(.*)\;/REPLACEMENT$1\;/){}
And yes, your way of setting a semicolon string is correct.

prex1
: Online tools for structural design
: Magnetic brakes for fun rides
: Air bearing pads
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top