The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Perl Programming
|
Index and whitespace
Discuss Index and whitespace in the Perl Programming forum on Dev Shed. Index and whitespace Perl Programming forum discussing coding in Perl, utilizing Perl modules, and other Perl-related topics. Perl, the Practical Extraction and Reporting Language, is the choice for many for parsing textual information.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 21st, 2013, 02:34 PM
|
|
|
|
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.
|

March 21st, 2013, 07:26 PM
|
 |
Contributing User
|
|
Join Date: Apr 2012
Location: spaceBAR Central
|
|
|

March 22nd, 2013, 06:56 AM
|
|
|
|
Thanks. I kind of figured the answer was no.
|

March 22nd, 2013, 08:01 AM
|
 |
kill 9, $$;
|
|
Join Date: Sep 2001
Location: Shanghai, An tSín
|
|
|
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.
|

March 22nd, 2013, 05:27 PM
|
|
|
|
Thanks, except I don't know of a way to use regex with index.
|

March 22nd, 2013, 05:54 PM
|
|
|
|
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.
|

March 22nd, 2013, 06:19 PM
|
|
|
|
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.
|

March 25th, 2013, 05:43 AM
|
 |
kill 9, $$;
|
|
Join Date: Sep 2001
Location: Shanghai, An tSín
|
|
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];
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|