Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > RE explanation

Reply
Thread Tools

RE explanation

 
 
Noname
Guest
Posts: n/a
 
      02-22-2005
Hi
I have following re
$Test="5-15";
my (@sid)= ($Test=~/(\d+)-(\d+)/?($1..$2):$_);
which polulate the @sid with 5 to 15 numbers,
what I don't understand is
?($1..$2):$_) how does this work

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      02-22-2005
"Noname" <(E-Mail Removed)> wrote in newsn0dyt26a9wbocu001
@news.europe.nokia.com:

> Hi
> I have following re
> $Test="5-15";
> my (@sid)= ($Test=~/(\d+)-(\d+)/?($1..$2):$_);
> which polulate the @sid with 5 to 15 numbers,
> what I don't understand is
> ?($1..$2):$_) how does this work


Please do not ask thousands of people to read the documentation for you.

From

perldoc perlop:

Conditional Operator
Ternary "?:" is the conditional operator, just as in C. It works much
like an if-then-else. If the argument before the ? is true, the
argument before the : is returned, otherwise the argument after the :
is returned.

Now, I personally would hope that I would never write anything like
this. Assuming $_ supposedly holds some default value, I would re-write
this as:

#! perl

use strict;
use warnings;

my $default = 1;
my $test = '5-15';
my @range;

if($test =~ /^(\d+)-(\d+)$/) {
@range = ($1 .. $2);
} else {
@range = ($default);
}

print "@range\n";

__END__

I don't know where the $test string is coming from, but you probably
want to perform some additional bounds checking on the variables. What
happens to your program if $test contains:

'0-2000000000'

Sinan
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple Explanation to Networking Wirelessly?? Jaxim Wireless Networking 4 08-19-2005 05:04 AM
explanation Mariusz VHDL 1 01-13-2004 02:10 AM
Debug message explanation Dean Cisco 1 01-07-2004 12:38 PM
DVMRP Explanation Acer0001 Cisco 0 11-20-2003 04:41 AM
Need Explanation Kaladhaur Palaniappa Perl 0 08-07-2003 09:47 AM



Advertisments