How to use split on spaces AND tabs?

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

  1. nospam

    nospam 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);


    -Thanks
    nospam, Jul 2, 2004
    #1
    1. Advertising

  2. nospam wrote:

    Hi nospam

    > The following split command will perform the
    > split on one or more spaces.


    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"

    > How can I perform
    > the split on one or more spaces AND tabs?


    Just use a character class instead of the single space
    /[ \t]+/
    Details see The Fine Manual

    > Or
    > for that matter, ANY whitespace?


    Did you Read The Fine Manual for the features you are using? Just use "\s""
    \s Match a whitespace character

    [...]
    > my @arr = split(/ +/, $_, 2);


    jue
    Jürgen Exner, Jul 2, 2004
    #2
    1. Advertising

  3. nospam

    Joe Smith Guest

    nospam wrote:

    > How can I perform
    > the split on one or more spaces AND tabs?


    RTFM. It's mentioned where split() is documented.

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


    my @arr = split ' ',$_,2;
    Joe Smith, Jul 3, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. qwweeeit
    Replies:
    2
    Views:
    646
    qwweeeit
    Dec 14, 2005
  2. ibloom
    Replies:
    12
    Views:
    895
    Bruno Desthuilliers
    Feb 13, 2008
  3. rantingrick

    Tabs -vs- Spaces: Tabs should have won.

    rantingrick, Jul 16, 2011, in forum: Python
    Replies:
    95
    Views:
    1,832
    Roy Smith
    Jul 19, 2011
  4. John Kopanas
    Replies:
    2
    Views:
    288
    Gregory Brown
    Jan 29, 2007
  5. Alex
    Replies:
    12
    Views:
    676
Loading...

Share This Page