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

Up front I must apologize for the trivial questions but I'm 2 years out of practice and have no Perl experts around to help.

First:
I need to split up a string on white space:
$var = "1 word here another word";

the "1 word here" needs to be split from "another word". I've tried several things with switch and split but nothing works. Also, once they are split up, how do I remove the white space either before or after the values. For example:
"1 word here " down to "1 word here" and
" another word" down to "another word"

Second:
I need to evaluate the first two positions of a string to see if they're numeric. I cannot remember how to specify to only look at certain positions in a string and nothing else. How is that done?

Thanks in advance for your help.
You guys rock!

Replies are listed 'Best First'.
Re: Split and Pattern Match
by particle (Vicar) on Jun 06, 2003 at 15:47 UTC
    1. my( @list )= spilt '' => $var; this trims whitespace automatically. check perlfunc for more on split
    2. $var =~ m/ \A ## beginning of string \d{2} ## two digit characters /x
      see perlre and perlretut for more on regular expressions.

    ~Particle *accelerates*

Re: Split and Pattern Match
by broquaint (Abbot) on Jun 06, 2003 at 15:46 UTC
    Here's a regex solution to your first query
    my $var = "1 word here another word"; my($head, $tail) = $var =~ /( (?:\w+[ ]?){3} ) [ ] (.*)/x; print "head - [$head]\n"; print "tail - [$tail]\n"; __output__ head - [1 word here] tail - [another word]
    For your second quandary we'll use another splashing of regex
    print "yup" if "12 buckle my shoe" =~ /^\d{2}\b/; __output__ yup
    See. perlre for more info on the regexes used above.
    HTH

    _________
    broquaint

Re: Split and Pattern Match
by hardburn (Abbot) on Jun 06, 2003 at 15:43 UTC

    I need to split up a string on white space

    my $var = "1 word here another word"; my @words = split /\s+/, $var;

    Also, once they are split up, how do I remove the white space either before or after the values.

    my $var = ' some words '; $var =~ s/\A \s* (.*) \s* \z /$1/x;

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: Split and Pattern Match
by djantzen (Priest) on Jun 06, 2003 at 15:54 UTC

    Does "1 word here another word" mean you have word pairs like "foo bar", "baz quux", or "foo bar baz quux"? Assuming the former, you can break that up like so:

    my $var = "foo bar"; my @vals = split ' ', $var; # if you need something more complex, use +a regex for the pattern

    Note that if you split on the whitespace it will already be stripped from the returned strings, so no extra processing should be necessary. For the second question, a match will do the trick:

    foreach my $val (@vals) { print "Numeric\n" if $val =~ /^[0-9]{2}/; }

    Alternatively you could use substr.


    "The dead do not recognize context" -- Kai, Lexx
Re: Split and Pattern Match
by wfbojar (Initiate) on Jun 06, 2003 at 16:29 UTC
    Thanks for the help. The white space did not come out on my orriginal post. It was supposed to be
    $var = "1 word here _______________________ another word";
    where the underscores are spaces. Thanks again.