Very useful advice, Shlomi; thanks!

> On Jun 2, 2018, at 3:57 AM, Shlomi Fish <shlo...@shlomifish.org> wrote:
> 
> On Fri, 1 Jun 2018 15:54:55 -0500
> Rick T <p...@reason.net <mailto:p...@reason.net>> wrote:
> 
>> This is a newbie question, I’m sure. But I get perplexed easily!
>> 
>> The follow code segment expects to receive a $student_id consisting of a
>> surname followed by a hyphen followed by a number. The die is for testing
>> what I’m doing. 
>> 
>> If I feed it 'jones-123’ it dies with ‘jones, - , 123’ as I expected. But if
>> I feed it ‘jones-‘ omitting the number on the end, it dies with no matches at
>> all (or blanks): ‘ , , ‘ and I cannot figure out why it does not die with
>> ‘jones, -, ' and would appreciate an explanation. Many thanks!
>> 
>>    $student_id = lc $student_info_hash{student_id}; # Impose lower case
>>    $student_id =~ s/\s//xmsg;                       # Remove whitespace
>> 
>>    $student_id =~
>>        m{
>>            \A([a-z]+)  # match and capture leading alphabetics 
>>            (-)         # hyphen to separate surname from student number
>>            ([0-9]+\z)  # match and capture trailing digits
>>         }xms;          # Perl Best Practices
>>    $student_surname = $1;
>>    my $hyphen       = $2;
>>    $student_number  = $3;
> 
> In addition to the other comments, Instead of that, you should do:
> 
> if (my ($surname, $hyphen, $num) = $student_id =~ m{ ... }xms)
> {
>       # match successful.
> }
> else
> {
>       die "match failed ...";
> }
> 
> always check for regex match successes and use the return captures of regex
> ops. See PBP for more.
> 
>> die "$student_surname, $hyphen, $student_number”;
>> 
>> Rick Triplett
>> 
> 
> 
> 
> -- 
> -----------------------------------------------------------------
> Shlomi Fish       http://www.shlomifish.org/ <http://www.shlomifish.org/>
> Best Introductory Programming Language - http://shlom.in/intro-lang 
> <http://shlom.in/intro-lang>
> 
> A horse! A horse! My kingdom for a horse!
>    — https://en.wikiquote.org/wiki/Richard_III_%28play%29 
> <https://en.wikiquote.org/wiki/Richard_III_%28play%29>
> 
> Please reply to list if it's a mailing list post - http://shlom.in/reply 
> <http://shlom.in/reply> .
> 
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org 
> <mailto:beginners-unsubscr...@perl.org>
> For additional commands, e-mail: beginners-h...@perl.org 
> <mailto:beginners-h...@perl.org>
> http://learn.perl.org/ <http://learn.perl.org/>

Reply via email to