#1
  1. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,496
    Rep Power
    594

    Index and whitespace


    I think I know the answer already but is there a way to use 'index' to find the first occurrence of white space? TIA.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Location
    spaceBAR Central
    Posts
    229
    Rep Power
    42
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,496
    Rep Power
    594
    Thanks. I kind of figured the answer was no.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  6. #4
  7. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3887
    Index only works for specific substrings, so if you were looking for a particular whitespace character, it would work. If you're searching for any whitespace, a regexp will be the way to go.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,496
    Rep Power
    594
    Thanks, except I don't know of a way to use regex with index.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2012
    Posts
    834
    Rep Power
    496
    You can't.

    Use either index (if you just need to find the specific space character (ASCII 32), i.e. the white space as typed by the space bar on a computer), and use a regex if you need to match the more general idea of white space (the \s metacharacter), i.e. either such a space or a tab, etc.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,496
    Rep Power
    594
    The problem is I need to know at what character position the first white space of any kind occurs. I'm thinking I need write a function to 'split' using a regexp for white space then return the length of the first string.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  14. #8
  15. kill 9, $$;
    Devshed Supreme Being (6500+ posts)

    Join Date
    Sep 2001
    Location
    Shanghai, An tSín
    Posts
    6,897
    Rep Power
    3887
    From the links that spacebar208 posted, you will see that the $-[0] variable will hold the offset of the beginning of the last successful regexp match. So you can achieve what you want with:
    Code:
    if ( $str =~ /\s/ ) {
       print $-[0];
    }

IMN logo majestic logo threadwatch logo seochat tools logo