Please make an executable code without the hash (just
use a variable) and with the DATA fed below __DATA__
in the script.  Then I would suggest putting an "if"
in front of the match line and print "It Matched.\n".

Maybe put a elsif with only 2 matches.

If you keep playing with it like this you can at least
figure out what it is doing, but maybe not why.  The
manpages will probably be needed to answer that.

Please let us know what you find out.


Mike



On 6/1/2018 3:55 PM, beginners-digest-h...@perl.org wrote:
Subject:
difficulty with matching
From:
Rick T <p...@reason.net>
Date:
6/1/2018 3:54 PM

To:
Perl Beginners <beginners@perl.org>


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;
die "$student_surname, $hyphen, $student_number”;

Rick Triplett

Reply via email to