0

In perl I want to achieve the following translation:

stmt1; gosub xyz;

to

stmt1; xyz();

How can I do this?

2
  • See comp.leeds.ac.uk/Perl/sandtr.html
    – Himanshu
    Dec 18, 2012 at 9:57
  • I tried to split the line using ";" as dilimiter and then if the pattern was found I tried to replace it. $line =~ m/$pattern_gosub(.*?)(\;)*$/; $line =~ s/$pattern_gosub//; $line =~ s/$1/$1()\;/; But this did not work Dec 18, 2012 at 10:26

3 Answers 3

1

The answers already given has provided the approximative answer, this will deal with your edge cases (missing semi-colons, additional clauses after semi-colons).

perl -plwe 's/\bgosub\s+([^;]+)/$1()/g'

It will match any sequence of characters after the gosub keyword followed by whitespace that are "not semi-colon" and remove them. I also added the /g global modifier, as it seems likely that you'd want to do all replacements possible on a single line. Note the use of word boundary \b to prevent partial matches, e.g. not replace legosub.

If the word boundary is not sufficient, e.g. it will replace 1.gosub because . causes a break between word characters, you can use a negative lookbehind instead:

perl -plwe 's/(?<![^;\s])gosub\s+([^;]+)/$1()/g'

This requires that any character before gosub is not anything except semi-colon or whitespace. Note that the double negation also allows for non-matches (beginning of line).

2
  • Thank you so much. It works for all the conditions that I have come across Dec 18, 2012 at 12:43
  • @user1912265 You're welcome. You know that you can mark an answer as "accepted" by clicking the check mark next to it, if you feel it has solved your problem.
    – TLP
    Dec 18, 2012 at 13:05
1

Run from the command line on the file you want to edit (replaceing file.ext):

perl -i.bk -pe 's/gosub (.*?);/$1()/g' file.ext
6
  • Thanks for your help. There are few lines in which the semi-colon is missing, what changes do I have to make in that case? Dec 18, 2012 at 10:28
  • Well you need something that will count as the end to your function name. If it's just a newline then you can use that: perl -i.bk -pe 's/gosub (.*?)\n/$1()\n/g' file.ext
    – Ilion
    Dec 18, 2012 at 10:32
  • It is possible the there might be another statement following the gosub. Sorry for not specifying that initially. So the line can be stmt1; gosub foo; stmt2; or it can also be just stmt1; gosub foo Dec 18, 2012 at 10:38
  • 1
    Why doesnt the following work? $line =~ s/gosub (.*?)[;\n\s]/$1();/; Dec 18, 2012 at 10:38
  • Make the last part optional by adding a question mark. $line =~ s/gosub (.*?)[;\n\s]?/$1();/
    – Himanshu
    Dec 18, 2012 at 10:40
0
my $str = 'stmt1; gosub xyz;';
$str    =~ s/gosub (.*?);?/$1();/;

print $str;
2
  • Thank you it worked. There are few lines in which the semi-colon is missing, what changes do I have to make in that case? Dec 18, 2012 at 10:23
  • I add ;? to regexp, it should help Dec 18, 2012 at 13:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.