Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Tidiest extract or define method?

by Peterpion (Acolyte)
on Mar 28, 2014 at 11:12 UTC ( [id://1080067]=perlquestion: print w/replies, xml ) Need Help??

Peterpion has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks. Been searching this morning for a tidier way to do this:
my $string = 'abcdef'; my $extracted = ''; if ($string =~ m/bce/) { $extracted = $1; } my $extracted2 = ''; if ($string =~ m/ced/) { $extracted2 = $1; }
etc. I've got a lot of these in a bit of code I have been working on and its taking a lot of space, so I would prefer to do something like
my ($extracted) = ($string =~ m/bce/)||('');
but that dosent work. Just thinking this must have been done a million times already and what the most tidy way others do it is.

TIA, Pete

Replies are listed 'Best First'.
Re: Tidiest extract or define method?
by BrowserUk (Patriarch) on Mar 28, 2014 at 11:25 UTC

    How about: my $ex = 'abcdef' =~ /(cde)/ ? $1 : '';?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Tidiest extract or define method?
by Don Coyote (Hermit) on Mar 28, 2014 at 11:26 UTC

    Hi Peterpion,

    the match operator returns undefined on a no-match. just assign the value and test for defined-ness on the variable

    my $string = 'abcde'; my ($ex)= $string =~ /fhi/; print 'my eleventh gender is ', defined $ex ? '' : 'not ', "defined\n";

    To run a few tests through, can pass into a subroutine to do the work, something like..

    #!/usr/bin/perl use warnings; use strict; sub get_tests{ my @tmp; for (0..(scalar(@_)-3)){ push @tmp, join('',@_[$_,$_+1,$_+2]); } return @tmp; } my $string = join('',('h'..'aq')); print $string,$/; my @tests = &get_tests('a' .. 'r'); my @genders = grep $string =~ /$_/, @tests; print join(' ', @genders),"\n"; exit 0;
Re: Tidiest extract or define method?
by AnomalousMonk (Archbishop) on Mar 28, 2014 at 19:16 UTC

    The  m//g (no capture,  /g modifier) or  m/(...)/ operator returns the empty list in list context on match failure, so one way to do what you want would be as follows (of course, you would replace  'nothing' with  '' the empty string):

    c:\@Work\Perl\monks>perl -wMstrict -le "for my $string (qw(abcdef ABCDEF xyzzy)) { my ($extracted) = ($string =~ m/cde|ABC/g, 'nothing'); print qq{from '$string' got '$extracted'}; } " from 'abcdef' got 'cde' from 'ABCDEF' got 'ABC' from 'xyzzy' got 'nothing'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1080067]
Approved by BrowserUk
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (2)
As of 2024-04-20 06:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found