CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
replacing multiple different characters - s///g;

 



Zeke_D
Deleted

Nov 3, 2000, 6:58 PM

Post #1 of 5 (773 views)
replacing multiple different characters - s///g; Can't Post

I am replacing the greaterthan character with an html string to add a specific color:
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


foreach $line (@data) {
$line =~ s/\>\>/<FONT COLOR=#FF0000>>>/g;
$line .= "<FONT COLOR=#000000>";
print "$line<BR>";
}
</pre><HR></BLOCKQUOTE>now, i want to change any line that contains 3 or more equal signs (===) to a <HR> how can I do this?

[This message has been edited by Zeke_D (edited 11-03-2000).]


sleuth
Enthusiast / Moderator

Nov 3, 2000, 9:25 PM

Post #2 of 5 (773 views)
Re: replacing multiple different characters - s///g; [In reply to] Can't Post

 
If you mean to substitute === or more in a row into an <hr> but leave the rest of the line alone, then do this.

$line =~ s!===+!<hr>!g;

If you want the entire line to change into an <hr> tag if there are three = anywhere in that line then do this.

if ($line =~ tr/=// >= 3){$line =~ s!^.*$!<hr>!g;}

I believe that will work.

Sleuth


Zeke_D
Deleted

Nov 3, 2000, 9:48 PM

Post #3 of 5 (773 views)
Re: replacing multiple different characters - s///g; [In reply to] Can't Post

Thanks, it worked perfectly.

You know, until i discovered perlguru.com, i was just a cgi hack, tweaking scripts to suit me, not quite understanding what everything does. Now, not only do I understand perl/cgi but I strive to become a better perl programmer because of the influence and ideas from this site. Now I can write scripts from scratch, and they do exactly what i want them to do, not what i could hack them to do.

The only problem with perl is that once you learn something, you start using it. :)

Special thanks to everyone out there who post replies - you make programming perl worth something.


japhy
Enthusiast

Nov 4, 2000, 5:27 AM

Post #4 of 5 (773 views)
Re: replacing multiple different characters - s///g; [In reply to] Can't Post

Then use the ^ anchor.

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


while ($line = get_next_line()) {
$line =~ s/^>/<font color="blue">>/ and
$line .= "</font>";
print $line;
}
</pre><HR></BLOCKQUOTE>

Something like that.

------------------
Jeff "japhy" Pinyan -- accomplished author, consultant, hacker, and teacher



Zeke_D
Deleted

Nov 4, 2000, 10:06 AM

Post #5 of 5 (773 views)
Re: replacing multiple different characters - s///g; [In reply to] Can't Post

I actually thought of another one:

$line =~ s|>|>|g;
$line =~ s|>|<FONT COLOR=#0000FF>>|g;
$line .= "</FONT>";

this works, by adding the font tag to the line.
I would rather have a line printed a paticular color only if it BEGINS with ">"

i know this is easy, but what do you expect from a reformed hack :)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives