> 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”;
([0-9]+\z)
means "capture one or more digits (same as "\d+") at the end of the
string/before a newline - so
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
zero or more digits
On Fri, Jun 1, 2018 at 3:54 PM, Rick T <[email protected]> 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;
> die "$student_surname, $hyphen, $student_number”;
>
> Rick Triplett
>
>
--
a
Andy Bach,
[email protected]
608 658-1890 cell
608 261-5738 wk