in reply to $1[
Hello rsFalse,
Well I am having some questions based on your code.
The sample of code that you provide us it gives me a different error (sample below):
#!/usr/bin/perl use warnings; use strict; $\ = $/; $_ = reverse "(a+b)*c"; # or some longer sequence s/\)([^()]+)\(/]$1[/g; print scalar reverse y/][/)(/r; __END__ $ perl test.pl Missing right curly or square bracket at test.pl line 10, within strin +g syntax error at test.pl line 10, at EOF Execution of test.pl aborted due to compilation errors.
Based on fellow Monk holli solution:
#!/usr/bin/perl use strict; use warnings; use feature 'say'; my $reversed = reverse "(a+b)*c"; # or some longer sequence $reversed =~ tr/()/[]/; my $str = reverse $reversed; say $str; __END__ $ perl test.pl [a+b]*c
Question: why you reverse the string? Am I missing something here? (Sample below):
#!/usr/bin/perl use strict; use warnings; use feature 'say'; my $test = "(a+b)*c"; $test =~ tr/()/[]/; say $test; __END__ $ perl test.pl [a+b]*c
An alternative way of resolving your issue (not more efficient - just for fun :D):
#!/usr/bin/perl use strict; use warnings; use feature 'say'; sub replaceChar { my ($str, $char, $replace) = @_; substr($str, index($str, $char), length($char), $replace); return $str; } my $s = "(a+b)*c"; my $leftParenthesis = "("; my $rightParenthesis = ")"; my $leftCurlyBracket = "["; my $rightCurlyBracket = "]"; my $str = replaceChar($s, $leftParenthesis, $leftCurlyBracket); say $str; say replaceChar($str, $rightParenthesis, $rightCurlyBracket); __END__ $ perl test.pl [a+b)*c [a+b]*c
BR / Thanos
Seeking for Perl wisdom...on the process of learning...not there...yet!
|
---|
In Section
Seekers of Perl Wisdom