By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,245 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,245 IT Pros & Developers. It's quick & easy.

Perl Split Pattern

P: 17
I am writing my very first Perl program, and am still lost with how to use split.
I have several lines of data that I want to read in, for example:

333444555 Smith Ana Ms RN
777666555 Jones Tom Mr MD

These data fields have certain restrictions. The first number always must be exactly 9 digits. The second and third can be up to 9 (anything less)..and the 3rd/4th must be exactly 2. There can be more than one space (or infinitely many spaces) as long as all data is on the same line. How could I use split here to determine this? Any suggestions you may have would be greatly appreciated!
Mar 31 '08 #1
Share this Question
Share on Google+
4 Replies


eWish
Expert 100+
P: 971
For split to be utilized you really need a delimiter. A delimiter can be just about anything ie: whitespace, tab, comma, pipe, dash and so on. If you don't have a common delimiter then I would suggest that you use a regex to capture the data.

Expand|Select|Wrap|Line Numbers
  1. my $string = '333444555 Smith Ana Ms RN'; 
  2. # Split the string on the whitespace
  3. my ($id, $lname, $fname, $suffix, $abrv) = split(/ /, $string);
--Kevin
Mar 31 '08 #2

Ganon11
Expert 2.5K+
P: 3,652
Well, assuming you can get the input line into a scalar, you can use split like this:

Expand|Select|Wrap|Line Numbers
  1. my @line_vals = split ' ', $the_input;
Now @line_vals will have (123456789, 'Lastname', 'Firstname', 'TITLE', 'OTHERVALUE') like your file, and you can perform checks and such on these values as normal.
Mar 31 '08 #3

Ganon11
Expert 2.5K+
P: 3,652
If you use a regexp like eWish suggests, make sure you account for the fact that more than one space might be there:

Expand|Select|Wrap|Line Numbers
  1. split / +/, $string;
I'm not sure if it matters - I don't know if split will ever return an empty string (for the items between spaces).
Mar 31 '08 #4

KevinADC
Expert 2.5K+
P: 4,059
split() will use spaces by default if no argument is used. This is perfectly valid:

Expand|Select|Wrap|Line Numbers
  1. while (<>) {
  2.    @foo = split;
  3. }
  4.  
it will correctly split a file with multiple spaces between fields and leave off leading/trailing spaces. I am pretty sure it also removes the line endings unlike split(/ /) or split(/ +/) because the default split is equivalent to split(/\s+/) . the \s character class includes tabs and newlines and maybe other things too, like carraige returns.
Mar 31 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.