Re: difficulty with matching

2018-06-04 Thread Shlomi Fish
On Sat, 2 Jun 2018 07:30:48 -0500
p...@reason.net wrote:

> Very useful advice, Shlomi; thanks!
> 

you are welcome! Also see http://perl-begin.org/tutorials/bad-elements/ , which
I also maintain.

> 
> > On Jun 2, 2018, at 3:57 AM, Shlomi Fish  wrote:
> > 
> > On Fri, 1 Jun 2018 15:54:55 -0500
> > Rick T 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/ 
> > Best Introductory Programming Language - http://shlom.in/intro-lang
> > 
> > 
> > A horse! A horse! My kingdom for a horse!
> >— https://en.wikiquote.org/wiki/Richard_III_%28play%29
> > 
> > 
> > Please reply to list if it's a mailing list post - http://shlom.in/reply
> >  .
> > 
> > --
> > To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> >  For additional commands, e-mail:
> > beginners-h...@perl.org 
> > http://learn.perl.org/   



-- 
-
Shlomi Fish   http://www.shlomifish.org/
NSA Factoids - http://www.shlomifish.org/humour/bits/facts/NSA/

Chuck Norris wrote an interpreter for a Turing-complete language using only
NOPs.
— http://www.shlomifish.org/humour/bits/facts/Chuck-Norris/

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: difficulty with matching

2018-06-03 Thread Mike Flannigan


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 
Date:
6/1/2018 3:54 PM

To:
Perl Beginners 


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




Re: difficulty with matching

2018-06-02 Thread perl
Very useful advice, Shlomi; thanks!


> On Jun 2, 2018, at 3:57 AM, Shlomi Fish  wrote:
> 
> On Fri, 1 Jun 2018 15:54:55 -0500
> Rick T 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/ 
> Best Introductory Programming Language - http://shlom.in/intro-lang 
> 
> 
> A horse! A horse! My kingdom for a horse!
>— https://en.wikiquote.org/wiki/Richard_III_%28play%29 
> 
> 
> Please reply to list if it's a mailing list post - http://shlom.in/reply 
>  .
> 
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org 
> 
> For additional commands, e-mail: beginners-h...@perl.org 
> 
> http://learn.perl.org/ 


Re: difficulty with matching

2018-06-02 Thread Shlomi Fish
On Fri, 1 Jun 2018 15:54:55 -0500
Rick T  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/
Best Introductory Programming Language - http://shlom.in/intro-lang

A horse! A horse! My kingdom for a horse!
— https://en.wikiquote.org/wiki/Richard_III_%28play%29

Please reply to list if it's a mailing list post - http://shlom.in/reply .

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: difficulty with matching

2018-06-01 Thread Andy Bach
 > 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  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,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk


Re: difficulty with matching

2018-06-01 Thread Octavian Rasnita
[0-9]+ means at least one digit in range 0-9.
If there is no digit, the string won't match.
You want [0-9]* instead.

--Octavian

  - Original Message - 
  From: Rick T 
  To: Perl Beginners 
  Sent: Friday, June 01, 2018 11:54 PM
  Subject: difficulty with matching


  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



difficulty with matching

2018-06-01 Thread Rick T
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