Heard about Perl much longer ago but never thought about looking into it until one day, I did a search for a backup script and found this Perl program. So I installed Perl and downloaded the script and it worked very well. I started to become curious now, but it looked difficult, wtf are all those special characters doing...
Then came a time that I needed find and replace something in a file and since I had Perl on my computer now I gave it a shot and started testing.
I went back in my archives and here are the two little gems:
while (<>) {
next if /^$/;
print;
}
and
$text = "A'B'C" ;
$text = replacequote($text);
print $text;
sub replacequote {
my $X=$_[0];
while ( $X =~ /\'/ ) {
(my $l, my $r) = split ( /\'/, $X, 2);
$X = "$l\\QUOTE$r";
}
while ( $X =~ /\\QUOTE/ ) {
(my $l, my $r) = split ( /\\QUOTE/, $X, 2);
$X = "$l\\'$r";
}
return $X;
}
sub replacequote_better {
my $X=$_[0];
while ( $X =~ /[^\\]\'/ ) {
#This line does not give the desired result that I have in mind (l rem
+ains empty):
(my $l, my $r) = split ( /[^\\]\'/, $X, 2);
#print "left: $l\n";
#print "right: $r\n";
@array = split ( /[^\\]+\'/, $X, 2);
print $_, "|" foreach @array;
print "\n" ;
$X = "$l\\'$r";
#print "new $X\n" ;
}
return $X;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.