Connecting Tech Pros Worldwide Forums | Help | Site Map

Perl Split Pattern

Newbie
 
Join Date: Sep 2007
Posts: 17
#1: Mar 31 '08
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!

eWish's Avatar
Moderator
 
Join Date: Jul 2007
Location: Arkansas
Posts: 900
#2: Mar 31 '08

re: Perl Split Pattern


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
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#3: Mar 31 '08

re: Perl Split Pattern


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.
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#4: Mar 31 '08

re: Perl Split Pattern


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).
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#5: Mar 31 '08

re: Perl Split Pattern


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.
Reply