Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE ^]/

by madM (Beadle)
on Apr 29, 2014 at 01:11 UTC ( [id://1084233]=note: print w/replies, xml ) Need Help??


in reply to Re: Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE ^]/ (solved)
in thread Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE ^]/

i know what it is.. the @aminos array is empty when the script runs... im starting the script with the command line using getopt::long i declare the array aminos at the first of the script and the other part is in a soubrutine.. so when i call the subroutine with the option sub ... the array @aminos is empty in the subroutine... do you know why is this happening? something like this:
GetOptions ( 'sub' => \& ) @aminos= qw (A R N D C Q E G H I L K M F P S T W Y V B Z X); sub a { if( "$substr1$substr2" =~ /[^@aminos]/ ) ... }
  • Comment on Re^2: Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE ^]/
  • Download Code

Replies are listed 'Best First'.
Re^3: Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE ^]/ (updated)
by LanX (Saint) on Apr 29, 2014 at 01:13 UTC
    > the @aminos array is empty when the script runs.

    yep I just deciphered it from the error msg.

    update

    > do you know why is this happening?

    the initialization of @aminos is never executed.

    using strict and warnings might have shown you.

    either put the initialization into the sub or declare a constant (string please!).

    update

    darn, constants are not interpolated in strings or regexes.

    you have to define a constant regex:

    DB<131> use constant REGEX=>qr/[^ABCD]/ DB<132> 'C' =~ REGEX DB<133> 'Z' =~ REGEX => 1

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      darn, constants are not interpolated in strings or regexes.

      As I understand it, even with use constant, a constant in Perl is an in line sub. IE:

      use constant PI => 4 * atan2(1, 1);

      is the short hand for

      sub PI { 4 * atan2(1, 1); }

      Which, after compile time constant expression evaluation, simplifies to:

      sub PI { 3.1415926536; # to the precision available on the machine running +the program }

      which the compiler can then in line where it would otherwise put a sub call.

        > where it would otherwise put a sub call.

        which isn't trivial within strings/regexes.

        the "@{[...]}" trick works but is IMHO a bit oversized for this task.

        DB<109> use constant STR => 1..3 DB<110> qr/[@{[ STR ]}]/ => qr/[1 2 3]/

        thats not interpolation anymore but inline eval.

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re^3: Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE ^]/
by AnomalousMonk (Archbishop) on Apr 29, 2014 at 08:08 UTC

    As mentioned before, you're invoking the subroutine (it's invoked during  GetOptions() processing) before the array is initialized. Eg.:

    c:\@Work\Perl\monks>perl -wMstrict -le "S('before'); ;; my @ra = qw(A C T G); ;; S('after'); ;; sub S { print qq{$_[0] initialization: [@ra] }; } " before initialization: [] after initialization: [A C T G]

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-20 14:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found