jb60606 Posted September 27, 2007 Share Posted September 27, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70826-solved-perl-regex-help/ Share on other sites More sharing options...
effigy Posted September 27, 2007 Share Posted September 27, 2007 A character class requires a match; ^[A][A-I]? would work. Quote Link to comment https://forums.phpfreaks.com/topic/70826-solved-perl-regex-help/#findComment-356507 Share on other sites More sharing options...
jb60606 Posted September 28, 2007 Author Share Posted September 28, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/70826-solved-perl-regex-help/#findComment-357148 Share on other sites More sharing options...
effigy Posted September 28, 2007 Share Posted September 28, 2007 I'm not entirely sure what you want. Should it report all matches or stop searching after the first is found? Where do you want the single characters to match? Quote Link to comment https://forums.phpfreaks.com/topic/70826-solved-perl-regex-help/#findComment-357167 Share on other sites More sharing options...
jb60606 Posted September 28, 2007 Author Share Posted September 28, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/70826-solved-perl-regex-help/#findComment-357308 Share on other sites More sharing options...
effigy Posted September 28, 2007 Share Posted September 28, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70826-solved-perl-regex-help/#findComment-357387 Share on other sites More sharing options...
jb60606 Posted September 28, 2007 Author Share Posted September 28, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70826-solved-perl-regex-help/#findComment-357524 Share on other sites More sharing options...
fenway Posted September 29, 2007 Share Posted September 29, 2007 - 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 @_. Quote Link to comment https://forums.phpfreaks.com/topic/70826-solved-perl-regex-help/#findComment-357693 Share on other sites More sharing options...
effigy Posted October 1, 2007 Share Posted October 1, 2007 - 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? Quote Link to comment https://forums.phpfreaks.com/topic/70826-solved-perl-regex-help/#findComment-359115 Share on other sites More sharing options...
fenway Posted October 1, 2007 Share Posted October 1, 2007 - 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. Quote Link to comment https://forums.phpfreaks.com/topic/70826-solved-perl-regex-help/#findComment-359135 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.