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 reply to Re: $1[
by thanos1983
in thread $1[
by rsFalse
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.