Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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

by madM (Beadle)
on Apr 29, 2014 at 00:31 UTC ( [id://1084221]=perlquestion: print w/replies, xml ) Need Help??

madM has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
Im getting this error when running my script and i donīt know why

Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE ^]/ <br/>
this is the fragment of the code where the error occurs
@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); if( "$substr1$substr2" =~ /[^@aminos]/ )


any ideas?

Replies are listed 'Best First'.
Re: Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE ^]/ (solved)
by LanX (Saint) on Apr 29, 2014 at 01:03 UTC
    > any ideas?

    not really, cause it does what you told it to do...

    DB<101> @a = 'a'..'d' => ("a", "b", "c", "d") DB<102> 'c' =~ /[^@a]/ DB<103> 'c' =~ /[@a]/ => 1 DB<104> $re=qr/[^@a]/ => qr/[^a b c d]/

    ... but wrongly, please note that whitespace is now excluded too!

    Apparently you're not showing the whole code.

    Are you sure about the content of @aminos?

    And why do you exclude 23 letters of the alphabet (in random order) instead of matching the remaining 3?

    edit

    your array @aminos is empty, thats why /[^]/ tries to exclude ']' within an unterminated character class.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      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]/ ) ... }
        > 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)

        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]
Re: Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE ^]/ (\@)
by tye (Sage) on Apr 29, 2014 at 00:32 UTC
    /[^\@aminos]/

    Update: Here is the node, as it was when I replied to it:

    Hi Monks,
    Im getting this error when running my script and i donīt know why

    Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE ^]/ <br/>
    this is the fragment of the code where the error occurs
    if( "$substr1$substr2" =~ /[^@aminos]/ )


    any ideas?

    - tye        

      why? can you explain?
Re: Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE ^]/
by monsoon (Pilgrim) on Apr 29, 2014 at 00:34 UTC
    @aminos gets interpreted as an array. 'use strict' would have shown it.
      thats what it should be interpreted.. it is an array with some letters in it

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 16:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found