How to use split on spaces AND tabs?

Discussion in 'Perl' started by Guest, Jul 2, 2004.

  1. Guest

    Guest Guest

    The following split command will perform the
    split on one or more spaces. How can I perform
    the split on one or more spaces AND tabs? Or
    for that matter, ANY whitespace? This reads input
    from a file, and you can't trust someone's text
    editor to enter tabs or spaces!

    my @arr = split(/ +/, $_, 2);

    Guest, Jul 2, 2004
    1. Advertisements

  2. nospam wrote:

    Hi nospam
    The first argument to split() is a good old plain regular expression. To
    find out more about regular expression please see The Fine Manual: "perldoc
    perlre" or for an introduction "perldoc perlretut"
    Just use a character class instead of the single space
    /[ \t]+/
    Details see The Fine Manual
    Did you Read The Fine Manual for the features you are using? Just use "\s""
    \s Match a whitespace character

    Jürgen Exner, Jul 2, 2004
    1. Advertisements

  3. Guest

    Joe Smith Guest

    RTFM. It's mentioned where split() is documented.
    my @arr = split ' ',$_,2;
    Joe Smith, Jul 3, 2004
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.