Oh god I hate perl - bloop bloop! [Resolved]

  • alex89
  • Bronze Member
  • Bronze Member
  • User avatar
  • Posts: 239
  • Loc: Western Australia

Post 3+ Months Ago

I'm trying to replace the string between forum brackets "[fish]text here[/fish]" with "boop! t boop! e boop! x boop! t h boop! e boop! r boop! e"

My attempt seems to have some problems. Any ideas?

PERL Code: [ Select ]
my $message = "start [fish]middle[/fish] end \n newline";
 
my $fish = $message;
print "\n1" . $message;
print "\n2" . $fish;
$fish =~ s~\[fish\](.+?)\[/fish\]~$1~isg;
 
print "\n3: " . $fish;
 
my $newfish;
 
for ($count = 0; $count < length ($newfish); $count++){
 
 $newfish .= substr ($fish, $count, 1)."blop!";
 
}
 
print $newfish;
 
$message =~ s~\[fish\](.+?)\[/fish\]~$newfish~isg;
 
print $message;
  1. my $message = "start [fish]middle[/fish] end \n newline";
  2.  
  3. my $fish = $message;
  4. print "\n1" . $message;
  5. print "\n2" . $fish;
  6. $fish =~ s~\[fish\](.+?)\[/fish\]~$1~isg;
  7.  
  8. print "\n3: " . $fish;
  9.  
  10. my $newfish;
  11.  
  12. for ($count = 0; $count < length ($newfish); $count++){
  13.  
  14.  $newfish .= substr ($fish, $count, 1)."blop!";
  15.  
  16. }
  17.  
  18. print $newfish;
  19.  
  20. $message =~ s~\[fish\](.+?)\[/fish\]~$newfish~isg;
  21.  
  22. print $message;


The same in javascript:

JAVASCRIPT Code: [ Select ]
var fish = message.match(/\[fish\](.+?)\[\/fish\]/ig);
 
if(fish != null) {
 
fish = fish[0];
 
fish = fish.replace( "[fish]", "");
fish = fish.replace( "[/fish]" , "");
 
var newfish = "";
 
for (var counter = 0;counter < fish.length ;counter ++ ) {
   newfish += fish.substring(counter-1, counter) + "bloop bloop!";
}
 
message = ubbcstr.replace(/\[fish\](.+?)\[\/fish\]/ig, newfish);
 
}
  1. var fish = message.match(/\[fish\](.+?)\[\/fish\]/ig);
  2.  
  3. if(fish != null) {
  4.  
  5. fish = fish[0];
  6.  
  7. fish = fish.replace( "[fish]", "");
  8. fish = fish.replace( "[/fish]" , "");
  9.  
  10. var newfish = "";
  11.  
  12. for (var counter = 0;counter < fish.length ;counter ++ ) {
  13.    newfish += fish.substring(counter-1, counter) + "bloop bloop!";
  14. }
  15.  
  16. message = ubbcstr.replace(/\[fish\](.+?)\[\/fish\]/ig, newfish);
  17.  
  18. }
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • mk27
  • Proficient
  • Proficient
  • User avatar
  • Posts: 334

Post 3+ Months Ago

PERL ROCKS!!!

One mistake you're caught up in is that the second half of the substitution has slightly different escape rules than the first half. For example, you must escape [] in the first half, but these are meaningless in the second so are fine as is UNLESS they follow a string variable...guess why:

$var[

uh-oh! that's an array subscription! You don't actually do that, but it is involved in the example below, so watch for that detail (line 13).

A mistake you do make is that / must be escaped in both halves, since that is the divider, altho you seem to use \ for some reason -- which is wrong.

So:
PERL Code: [ Select ]
#!/usr/bin/perl -w
use strict;
 
# simple replacement
my $string = "[fish]text here[/fish]\n";
$string =~ s/\[fish\].*\[\/fish\]/[fish]boop boop[\/fish]/g;
print $string;
 
# replacement involving intermediate process
$string = "[fish]text here[/fish]\n";
$string =~ /\[fish\](.*)\[\/fish\]/;
my $new = uc($1);
$string =~ s/\[fish\].*\[\/fish\]/[fish]$new\[fish]/g;
print $string;
 
  1. #!/usr/bin/perl -w
  2. use strict;
  3.  
  4. # simple replacement
  5. my $string = "[fish]text here[/fish]\n";
  6. $string =~ s/\[fish\].*\[\/fish\]/[fish]boop boop[\/fish]/g;
  7. print $string;
  8.  
  9. # replacement involving intermediate process
  10. $string = "[fish]text here[/fish]\n";
  11. $string =~ /\[fish\](.*)\[\/fish\]/;
  12. my $new = uc($1);
  13. $string =~ s/\[fish\].*\[\/fish\]/[fish]$new\[fish]/g;
  14. print $string;
  15.  


Using an editor that has good syntax highlighting for perl helps a lot with this (the highlighting here is relatively crude and does not cover the regexp)...an alternative for line 6 is:
Code: [ Select ]
$string =~ s/(\[fish\]).*(\[\/fish\])/$1boop boop$2/g;
  • alex89
  • Bronze Member
  • Bronze Member
  • User avatar
  • Posts: 239
  • Loc: Western Australia

Post 3+ Months Ago

Ah thank you, I get it now.

My final code:

PERL Code: [ Select ]
$message=~ /\[fish\](.*)\[\/fish\]/;
my $fish = $1; #I thought you could do this in one line
 
my $newfish;
 
for ($count = 0; $count < length ($fish); $count++){
   $newfish .= substr ($fish, $count, 1)." bloop bloop! ";
}
 
$message =~ s~\[fish\](.+?)\[/fish\]~$newfish~isg;
  1. $message=~ /\[fish\](.*)\[\/fish\]/;
  2. my $fish = $1; #I thought you could do this in one line
  3.  
  4. my $newfish;
  5.  
  6. for ($count = 0; $count < length ($fish); $count++){
  7.    $newfish .= substr ($fish, $count, 1)." bloop bloop! ";
  8. }
  9.  
  10. $message =~ s~\[fish\](.+?)\[/fish\]~$newfish~isg;

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 167 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.