Jump to content

[SOLVED] Perl: REGEX help


jb60606

Recommended Posts

I'm working on a Perl script that asks the user for a symbol and, using regex, determines what subgroup of a "hash" the symbol belongs to.

%sbfeeds = (
                 superbook1 => ["^[A][A-I]", "^[A][J-Z]", "^[b]", "^[C][A-M]"],
                 superbook2 => ["^[C][N-Z]", "^[D]", "^[E]", "^[F]"],
                 superbook3 => ["^[G]", "^[H]", "^[i]"],
                 superbook4 => ["^[JK]", "^[L]", "^[M][A-L]", "^[M][M-Z]", "^[N]"],
                 superbook5 => ["^[O]", "^[P]", "^[QR]", "^[s][A-N]"],
                 superbook6 => ["^[s][O-Z]", "^[T]", "^[u-W]", "^[X-Z]"]
);

do {
                 print "Enter the symbol: ";
                 chomp($symbol = <>);
                 $symbol = uc($symbol);

                 foreach my $group(keys %sbfeeds){
                 foreach (@{$sbfeeds{$group}}) {
                 if ($symbol =~ $_){
                 print "$symbol found in $group\n";
}
}
}

                 print "\nContinue? (y/n): ";
                 chomp($continue = <>);
                 $continue = uc($continue);

} until ($continue eq "N");

exit 0;

 

 

So, for example, if the user types in the symbol "AMD", it will tell them that the symbol belongs to "superbook1". If the user types in "SIRI", it'll tell them "superbook6".

 

The problem is with the single character symbols A, C, M & S -- they aren't being recognized. They all have one thing in common; they all appear twice.

 

[A][A-I]     [A][J-Z]

[C][A-M]    [C][N-Z]

[M][A-L]    [M][M-Z]

etc

 

Im somewhat new to perl, and very new to regex. Does anyone have any ideas?

 

Thanks in advance.

Link to comment
Share on other sites

Not sure if I know what you mean, but adding a question mark to the end of each resulted in symbols being found in all groups where a single letter of the symbol exists.

 

For example:

 

Enter the symbol: a

A found in superbook1

 

Continue? (y/n):

Enter the symbol: amd

AMD found in superbook2

AMD found in superbook1

AMD found in superbook1

AMD found in superbook4

 

 

any ideas?

Link to comment
Share on other sites

nevermind effigy. I'm an idiot.

 

My regex was just missing representation for the single letters: A, C, M & S.

 

For some reason, in another script I use, I only had to use the previous regex for it to recognize the single letters.

 

New Regex is as follows:

 

 %sbfeeds = (
        superbook1 => ["^[A]\$", "^[A][A-I]", "^[A][J-Z]", "^[b]", "^[C]\$", "^[C][A-M]"],
        superbook2 => ["^[C][N-Z]", "^[D]", "^[E]", "^[F]"],
        superbook3 => ["^[G]", "^[H]", "^[i]"],
        superbook4 => ["^[JK]", "^[L]", "^[M]\$", "^[M][A-L]", "^[M][M-Z]", "^[N]"],
        superbook5 => ["^[O]", "^[P]", "^[QR]", "^[s]\$", "^[s][A-N]"],
        superbook6 => ["^[s][O-Z]", "^[T]", "^[u-W]", "^[X-Z]"],
        );

 

Thanks

Link to comment
Share on other sites

Since you mentioned that you are somewhat new to Perl, here are a few pointers:

 

- Always use strict; and use warnings;.

- Use the proper quotes. Since your patterns are not interpolating variables use single quotes instead of double. This will add clarity to the code and you won't need to escape the $.

- You can use for as an alias for foreach. It saves some typing.

Link to comment
Share on other sites

Thanks effigy; I appreciate any and all tips. Perl is probably the first language I am trying to learn thoroughly (as it is becoming more of a necessity within my role at work).

 

I've been told numerous times to use the "strict" pragma, but laziness always gets the best of me; especially when I'm on a deadline or when problems like this regex issue arise & consume so much time. Usually I'm so ecstatic to get the the script working that -- if there are any potential issues that don't affect the the script's ability to function --I don't want to see them.

 

As of late, though, I have been using it more often especially with smaller scripts. I just read that you can declare all private variables at the top of the script in one line (ie: my ($var1, $var2, $var3, $var4, $var5), etc), which definately makes the transition a lot easier.

 

Thanks again.

Link to comment
Share on other sites

- Always use strict; and use warnings;.

Maybe when you're testing, but never in production...

 

As of late, though, I have been using it more often especially with smaller scripts. I just read that you can declare all private variables at the top of the script in one line (ie: my ($var1, $var2, $var3, $var4, $var5), etc), which definately makes the transition a lot easier.

Actually, that's most useful for assignment of @_.

Link to comment
Share on other sites

- Always use strict; and use warnings;.

Maybe when you're testing, but never in production...

 

There should be %SIG handlers set up to properly handle the logging and/or display of warnings/errors, but it's my impression that you never turn them off. Do you have any resources on this matter fenway?

Link to comment
Share on other sites

- Always use strict; and use warnings;.

Maybe when you're testing, but never in production...

 

There should be %SIG handlers set up to properly handle the logging and/or display of warnings/errors, but it's my impression that you never turn them off. Do you have any resources on this matter fenway?

Actually, it's not for the errors... the "strict" module is rather large, and once you've established that there are no "unstrict" code blocks, there's no point have it there all the time, it's expensive during compile-time.  Besides, I often write code that requires me to turn strict off.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.