RE explanation

Discussion in 'Perl Misc' started by Noname, Feb 22, 2005.

  1. Noname

    Noname Guest

    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
     
    Noname, Feb 22, 2005
    #1
    1. Advertising

  2. "Noname" <> wrote in news:xn0dyt26a9wbocu001
    @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
     
    A. Sinan Unur, Feb 22, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mariusz

    explanation

    Mariusz, Jan 12, 2004, in forum: VHDL
    Replies:
    1
    Views:
    614
    tbx135
    Jan 13, 2004
  2. Kaladhaur Palaniappa

    Need Explanation

    Kaladhaur Palaniappa, Aug 7, 2003, in forum: Perl
    Replies:
    0
    Views:
    1,028
    Kaladhaur Palaniappa
    Aug 7, 2003
  3. Benjie Fallar

    tracing output explanation

    Benjie Fallar, Jul 15, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    373
    Benjie Fallar
    Jul 15, 2003
  4. George Ter-Saakov

    Do you have an explanation for this?

    George Ter-Saakov, Apr 29, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    614
    Pravin A. Sable
    Apr 30, 2004
  5. Dave
    Replies:
    4
    Views:
    408
Loading...

Share This Page