Ryan Moszynski wrote:
> I need to write some code to allow users to specify which of a whole
> bunch of elements(e.g.512/1024) that they want to view.  My idea for
> how to do this was to have them input a semicolon delimited list, for
> example:
> 
> 1-10;25;33;100-250
> 
> 
> i tried using this to check to make sure they input a valid list that
> i can process:
> ###########
>        foreach ($temp2 = <>) {
> 
>     $list1 = $temp2;

You are assigning a scalar from <> to $temp2 and then foreach is assigning
that same value to $_ and then you are assigning that same value to $list1.
How many variables do you need to hold the same value?  Perhaps you should use:

while ( my $list1 = <> ) {


>     if ($list1 =~ /(\s*\d+;)+/g || $list1 =~ /(\s*\d+;)+/g ) {

>From your example above the pattern matches the three substrings:

1-10;25;33;100-250
  ^^^+++^^^

And after the match $1 contains '33;'


>         print "yay\n";
>     }else {print "boo\n";};
> 
>     #print "...",$list1, "...\n";
> 
>    }
> ###########
> 
> which doesn't work, because as soon as it matches the first time,
> anything goes.  How do i get it check for repetition, even though i
> don't know how many repetitions there will be.  there could be
> 1,2,3,5, even 10 groupings.
> 
> so the pattern isns't hard, there has to be a number, then either a
> '-' or s ';', then repeat or not.  the only special case is the first
> one which could just be a single number, or a number '-'number.  I
> just don't know how to implement it.
> 
> (#(-||;))(#(-||;))(#(-||;))

You probably want:

while ( my $list1 = <> ) {
    chomp $list1;
    if ( $list1 =~ /^(?:\d[\d;-]*\d|\d)$/;
        print "yay\n";
        }
    else {
        print "boo\n";
        }

    #print "...$list1...\n";
    }



John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to