Tip: This is a beginners list, therefore many questions will be simple. Aim for more descriptive subject lines and life will be easier for users of the list archives.

On 16 Jun 2004, at 17:10, Kevin Zhang wrote:
For the following string:

"uuuu axyzb oooo cxyzd vvvv"

What is the command to extract the substrings with "xyz" in them? In this case, I'd like to get two strings "axyzb" and "cxyzd".

The useful functions here are "grep" and "split" (perdoc -f grep and so on).


#!/usr/bin/perl

use strict;
use warnings;

my $string = "uuuu axyzb oooo cxyzd vvvv";

my @list_of_words = split /\ /, $string;

my @list_of_words_containing_xyz = grep /xyz/, @list_of_words;

foreach my $word (@list_of_words_containing_xyz) {
        print $word, "\n";
}

or, in less verbose form:

foreach (grep(/xyz/,split(/\ /, "uuuu axyzb oooo cxyzd vvvv"))) { print $_, "\n"; }

--
David Dorward
     <http://dorward.me.uk/>
<http://blog.dorward.me.uk/>


-- 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