Re: Recursive Validation Function

2014-01-02 Thread John Aten
Hello again,

I wasn't able to continue with the project I was working on way back in 
September, but I wanted to send a quick note to thank everyone (Andy Bach, Jim 
Gibson, Rob Dixon and Mike Flannigan) for their help. I apologize for not 
following up sooner. Hopefully I'll have some time to get back to it some day 
soon; I was a little optimistic in my planning.  Thanks again!

On Sep 4, 2013, at 10:55 PM, beginners-digest-h...@perl.org wrote:

> 
> beginners Digest 5 Sep 2013 03:55:27 - Issue 4576
> 
> Topics (messages 123410 through 123433):
> 
> Re: Recursive Validation Function
>   123410 by: Andy Bach
>   123411 by: Jim Gibson
>   123412 by: Rob Dixon
>   123413 by: Rob Dixon
>   123427 by: Mike Flannigan
> 
> 
> --
> 
> From: Andy Bach 
> Date: September 2, 2013 8:43:59 PM CDT
> To: John Aten 
> Cc: "beginners@perl.org" 
> Subject: Re: Recursive Validation Function
> 
> 
> 
> 
> On Monday, September 2, 2013, John Aten wrote:
> my $valid_token = validate_tokens($token);
> 
> 
> Too bad it doesn't work! Even if I put in valid tokens on the first shot, 
> there are errors:
> 
> You're passing the token as a parameter to the sub but the is using $_ for 
> the match. You need to assign the parameter in the sub. They arrive on the 
> global @_ array; one idiom is
> sub validate_token {
>   my ($test_token) = @_;
>   
> This assigns the first passed parameter to test_token. The advantage being of 
> you add more params you can just insert a new var. in the LHS list:
>   my ($test_token, $empty_allowed, $cleanup) = @_;
> 
> Now use $test_token instead of $_
> 
>  
> 
> I am writing a script to rename files. The script prompts the user to enter 
> tokens which will comprise part of the file name. These are made of letters 
> and numbers separated by a dot, ie: R.3. The letters can be R, I or C, upper 
> or lowercase. The first number can be one through eight, and if it is a six 
> there can be an additional dot followed by a one or two. To make sure that I 
> don't make any mistakes when putting in these tokens, I have tried to write a 
> function to make sure that they are valid:
> 
> sub validate_tokens {
> if ($_ !~ /^[ric]\.[1234578]$/i) {
> if ($_ !~ /^[ric]\.6(\.[1|2])?$/i) {
> print "Enter valid tokens: ";
> my $new_token = ;
> chomp $new_token;
> validate_tokens($new_token);
> }
> }
> return $_;
> }
> 
> 
> I'm calling it like this:
> 
> print "Enter Tokens: ";
> my $token = ;
> chomp $token;
> my $valid_token = validate_tokens($token);
> 
> 
> Too bad it doesn't work! Even if I put in valid tokens on the first shot, 
> there are errors:
> 
> Use of uninitialized value $_ in pattern match (m//) at bulk.pl line 65, 
>  line 3.
> Use of uninitialized value $_ in pattern match (m//) at bulk.pl line 66, 
>  line 3.
> 
> Lines 65 and 66 are the ones with the two if statements, lines 2 and 3 of the 
> first code snippet above. Each time I run it, the  line number in the 
> error message increases by one. The script goes into an infinite loop, 
> prompting for valid tokens, but even if valid ones are entered it continues. 
> What am I missing?
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
> 
> 
> 
> 
> -- 
> 
> a
> 
> Andy Bach,
> afb...@gmail.com
> 608 658-1890 cell
> 608 261-5738 wk
> 
> 
> 
> From: Jim Gibson 
> Date: September 3, 2013 12:56:16 AM CDT
> To: "begin >> Perl Beginners" 
> Subject: Re: Recursive Validation Function
> 
> 
> 
> On Sep 2, 2013, at 6:23 PM, John Aten wrote:
> 
>> Hello all,
>> 
>> I am writing a script to rename files. The script prompts the user to enter 
>> tokens which will comprise part of the file name. These are made of letters 
>> and numbers separated by a dot, ie: R.3. The letters can be R, I or C, upper 
>> or lowercase. The first number can be one through eight, and if it is a six 
>> there can be an additional dot followed by a one or two. To make sure that I 
>> don't make any mistakes when putting in these tokens, I have tried to write 
>> a function to make sure that they are valid:
> 
> Use $_[0] instead of $_ or the other suggestions from Andy.
> 
>> 
>> sub validate

Re: Recursive Validation Function

2013-09-04 Thread Mike Flannigan


On 9/2/2013 8:23 PM, John wrote:


Hello all,

I am writing a script to rename files. The script prompts the user to enter 
tokens which will comprise part of the file name. These are made of letters and 
numbers separated by a dot, ie: R.3. The letters can be R, I or C, upper or 
lowercase. The first number can be one through eight, and if it is a six there 
can be an additional dot followed by a one or two. To make sure that I don't 
make any mistakes when putting in these tokens, I have tried to write a 
function to make sure that they are valid:

sub validate_tokens {
if ($_ !~ /^[ric]\.[1234578]$/i) {
if ($_ !~ /^[ric]\.6(\.[1|2])?$/i) {
print "Enter valid tokens: ";
my $new_token = ;
chomp $new_token;
validate_tokens($new_token);
}
}
return $_;
}


I'm calling it like this:

print "Enter Tokens: ";
my $token = ;
chomp $token;
my $valid_token = validate_tokens($token);


Too bad it doesn't work! Even if I put in valid tokens on the first shot, there 
are errors:

Use of uninitialized value $_ in pattern match (m//) at bulk.pl line 65, 
 line 3.
Use of uninitialized value $_ in pattern match (m//) at bulk.pl line 66, 
 line 3.

Lines 65 and 66 are the ones with the two if statements, lines 2 and 3 of the first 
code snippet above. Each time I run it, the  line number in the error 
message increases by one. The script goes into an infinite loop, prompting for valid 
tokens, but even if valid ones are entered it continues. What am I missing?


Howdy.

In your subroutine replace "$_" with "$_[0]" in
3 places.  Then it may still be broke, but it
will function better.


Mike





Re: Recursive Validation Function

2013-09-03 Thread Rob Dixon

On 03/09/2013 06:56, Jim Gibson wrote:


For readability, use the extended form and the more modern
zero-width  assertions \A and \z:

if ( $_[0] !~ m{ \A [ric] \. (?:[1-578]) | (?:6 (\.[12])? ) \z }ix ) {


Hi Jim

You have incorrect parentheses. Your regex matches

\A [ric] \. (?:[1-578])

or

(?:6 (\.[12])? ) \z

which isn't what is wanted at all!

Rob

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




Re: Recursive Validation Function

2013-09-03 Thread Rob Dixon

On 03/09/2013 02:23, John Aten wrote:

Hello all,

I am writing a script to rename files. The script prompts the user
to enter tokens which will comprise part of the file name. These are
 made of letters and numbers separated by a dot, ie: R.3. The
letters can be R, I or C, upper or lowercase. The first number can be
one through eight, and if it is a six there can be an additional dot
followed by a one or two. To make sure that I don't make any
mistakes when putting in these tokens, I have tried to write a
function to make sure that they are valid:

sub validate_tokens {
if ($_ !~ /^[ric]\.[1234578]$/i) {
if ($_ !~ /^[ric]\.6(\.[1|2])?$/i) {
print "Enter valid tokens: ";
my $new_token = ;
chomp $new_token;
validate_tokens($new_token);
}
}
return $_;
}


I'm calling it like this:

print "Enter Tokens: ";
my $token = ;
chomp $token;
my $valid_token = validate_tokens($token);


Too bad it doesn't work! Even if I put in valid tokens on the first shot, there 
are errors:

Use of uninitialized value $_ in pattern match (m//) at bulk.pl line 65, 
 line 3.
Use of uninitialized value $_ in pattern match (m//) at bulk.pl line 66, 
 line 3.

Lines 65 and 66 are the ones with the two if statements, lines 2 and
3 of the first code snippet above. Each time I run it, the 
line number in the error message increases by one. The script goes
into an infinite loop, prompting for valid tokens, but even if valid
ones are entered it continues. What am I missing?


As has been explained, the parameter to your subroutine is in @_. $_ 
could contain anything, and clearly is undefined at this point.


It is also a misuse of recursion as it stands. You should use the 
subroutine just for verification, and put a reprompt loop outside it.


Try this for your subroutine

sub validate_tokens {
  $_[0] =~ /^[ric]\.(?:6\.[12]|[1-8])$/i;
}

and call it like this

my $token;
print "Enter Tokens: ";
while () {
  $token = ;
  chomp $token;
  last if validate_tokens($token);
  print "Enter valid tokens: ";
}

HTH,

Rob


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




Re: Recursive Validation Function

2013-09-02 Thread Jim Gibson

On Sep 2, 2013, at 6:23 PM, John Aten wrote:

> Hello all,
> 
> I am writing a script to rename files. The script prompts the user to enter 
> tokens which will comprise part of the file name. These are made of letters 
> and numbers separated by a dot, ie: R.3. The letters can be R, I or C, upper 
> or lowercase. The first number can be one through eight, and if it is a six 
> there can be an additional dot followed by a one or two. To make sure that I 
> don't make any mistakes when putting in these tokens, I have tried to write a 
> function to make sure that they are valid:

Use $_[0] instead of $_ or the other suggestions from Andy.

> 
> sub validate_tokens {
>   if ($_ !~ /^[ric]\.[1234578]$/i) {
>   if ($_ !~ /^[ric]\.6(\.[1|2])?$/i) {

The '[1|2]' part of this regular expression is incorrect. Characters in a 
character class do not need the pipe character for alternation. They are 
already treated as alternates. This part should be '[12]'. Your pattern will 
match the string 'r.6|'

You can combine the two regular expressions by using alternation and 
non-capturing grouping (clustering):

if( $_[0] !~ /^[ric]\.(?:[1-578])|(?:6(\.[12])?)$/i ) {
  ...

For readability, use the extended form and the more modern zero-width 
assertions \A and \z:

if( $_[0] !~ m{ \A [ric] \. (?:[1-578]) | (?:6 (\.[12])? ) \z }ix ) {
  ...

>   print "Enter valid tokens: ";
>   my $new_token = ;
>   chomp $new_token;
>   validate_tokens($new_token);
>   } 
>   }
>   return $_;
> }
> 
> 
> I'm calling it like this:
> 
>   print "Enter Tokens: ";
>   my $token = ;
>   chomp $token;
>   my $valid_token = validate_tokens($token);
> 
> 
> Too bad it doesn't work! Even if I put in valid tokens on the first shot, 
> there are errors:
> 
> Use of uninitialized value $_ in pattern match (m//) at bulk.pl line 65, 
>  line 3.
> Use of uninitialized value $_ in pattern match (m//) at bulk.pl line 66, 
>  line 3.
> 
> Lines 65 and 66 are the ones with the two if statements, lines 2 and 3 of the 
> first code snippet above. Each time I run it, the  line number in the 
> error message increases by one. The script goes into an infinite loop, 
> prompting for valid tokens, but even if valid ones are entered it continues. 
> What am I missing?
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
> 
> 


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




Re: Recursive Validation Function

2013-09-02 Thread Andy Bach
On Monday, September 2, 2013, John Aten wrote:
my $valid_token = validate_tokens($token);


Too bad it doesn't work! Even if I put in valid tokens on the first shot,
there are errors:

You're passing the token as a parameter to the sub but the is using $_ for
the match. You need to assign the parameter in the sub. They arrive on the
global @_ array; one idiom is
sub validate_token {
  my ($test_token) = @_;

This assigns the first passed parameter to test_token. The advantage being
of you add more params you can just insert a new var. in the LHS list:
  my ($test_token, $empty_allowed, $cleanup) = @_;

Now use $test_token instead of $_



>
> I am writing a script to rename files. The script prompts the user to
> enter tokens which will comprise part of the file name. These are made of
> letters and numbers separated by a dot, ie: R.3. The letters can be R, I or
> C, upper or lowercase. The first number can be one through eight, and if it
> is a six there can be an additional dot followed by a one or two. To make
> sure that I don't make any mistakes when putting in these tokens, I have
> tried to write a function to make sure that they are valid:
>
> sub validate_tokens {
> if ($_ !~ /^[ric]\.[1234578]$/i) {
> if ($_ !~ /^[ric]\.6(\.[1|2])?$/i) {
> print "Enter valid tokens: ";
> my $new_token = ;
> chomp $new_token;
> validate_tokens($new_token);
> }
> }
> return $_;
> }
>
>
> I'm calling it like this:
>
> print "Enter Tokens: ";
> my $token = ;
> chomp $token;
> my $valid_token = validate_tokens($token);
>
>
> Too bad it doesn't work! Even if I put in valid tokens on the first shot,
> there are errors:
>
> Use of uninitialized value $_ in pattern match (m//) at bulk.pl line 65,
>  line 3.
> Use of uninitialized value $_ in pattern match (m//) at bulk.pl line 66,
>  line 3.
>
> Lines 65 and 66 are the ones with the two if statements, lines 2 and 3 of
> the first code snippet above. Each time I run it, the  line number
> in the error message increases by one. The script goes into an infinite
> loop, prompting for valid tokens, but even if valid ones are entered it
> continues. What am I missing?
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org 
> For additional commands, e-mail: beginners-h...@perl.org 
> http://learn.perl.org/
>
>
>

-- 

a

Andy Bach,
afb...@gmail.com
608 658-1890 cell
608 261-5738 wk


Recursive Validation Function

2013-09-02 Thread John Aten
Hello all,

I am writing a script to rename files. The script prompts the user to enter 
tokens which will comprise part of the file name. These are made of letters and 
numbers separated by a dot, ie: R.3. The letters can be R, I or C, upper or 
lowercase. The first number can be one through eight, and if it is a six there 
can be an additional dot followed by a one or two. To make sure that I don't 
make any mistakes when putting in these tokens, I have tried to write a 
function to make sure that they are valid:

sub validate_tokens {
if ($_ !~ /^[ric]\.[1234578]$/i) {
if ($_ !~ /^[ric]\.6(\.[1|2])?$/i) {
print "Enter valid tokens: ";
my $new_token = ;
chomp $new_token;
validate_tokens($new_token);
} 
}
return $_;
}


I'm calling it like this:

print "Enter Tokens: ";
my $token = ;
chomp $token;
my $valid_token = validate_tokens($token);


Too bad it doesn't work! Even if I put in valid tokens on the first shot, there 
are errors:

Use of uninitialized value $_ in pattern match (m//) at bulk.pl line 65, 
 line 3.
Use of uninitialized value $_ in pattern match (m//) at bulk.pl line 66, 
 line 3.

Lines 65 and 66 are the ones with the two if statements, lines 2 and 3 of the 
first code snippet above. Each time I run it, the  line number in the 
error message increases by one. The script goes into an infinite loop, 
prompting for valid tokens, but even if valid ones are entered it continues. 
What am I missing?
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/