Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: $1[

by thanos1983 (Parson)
on Oct 11, 2017 at 08:58 UTC ( #1201149=note: print w/replies, xml ) Need Help??

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 Missing right curly or square bracket at line 10, within strin +g syntax error at line 10, at EOF Execution of 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 [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 [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 [a+b)*c [a+b]*c

BR / Thanos

Seeking for Perl wisdom...on the process of learning...not there...yet!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1201149]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2021-01-19 14:19 GMT
Find Nodes?
    Voting Booth?