Hi shawn,

On Wed, Nov 21, 2012 at 11:48 PM, shawn wilson <ag4ve...@gmail.com> wrote:

> how do i return something when i've got a long regex and one of the
> captures is empty?
>
> $_ = '"435" "" "634";
> my ($one, $two, $three)= /^
>    "(\d+)"\
>    "(\d+)"\
>    "(\d+)"
> /x;
>

    You can do like so:

$_ = '"435" "" "634"';

if ( my ( $one, $two, $three ) = /\"(.*?)\"/sg ) {
    print join "\n" => ( $one, $two, $three );
}

Please note the use of '=', since we are using regex on '$_' as the string.
Clear and better practice is to write like so:

   $_ = '"435" "" "634"';

if ( my ( $one, $two, $three ) = $_ =~ m/\"(.*?)\"/sg ) {
    print join "\n" => ( $one, $two, $three );
}

Moreover, if you have the module Data::Printer from CPAN installed, the out
dispaly will be more meaningful. like so:

use Data::Printer;

$_ = '"435" "" "634"';

if ( my @arr = /\"(.*?)\"/sg ) {
    p @arr;
}

OUTPUT

[
    [0] 435,
    [1] "",
    [2] 634
]

Data::Printer ->
http://search.cpan.org/~garu/Data-Printer-0.34/lib/Data/Printer.pm


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


-- 
Tim

Reply via email to