473,395 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Perl Split Pattern

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
4 1845
eWish
971 Expert 512MB
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
3,652 Expert 2GB
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
3,652 Expert 2GB
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
4,059 Expert 2GB
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

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

Similar topics

5
by: Stu Cazzo | last post by:
I have the following: String myStringArray; String myString = "98 99 100"; I want to split up myString and put it into myStringArray. If I use this: myStringArray = myString.split(" "); it...
9
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # Matching string patterns # # Sometimes you want to know if a string is of # particular pattern. Let's say in your website # you have converted all images...
31
by: surfunbear | last post by:
I've read some posts on Perl versus Python and studied a bit of my Python book. I'm a software engineer, familiar with C++ objected oriented development, but have been using Perl because it is...
3
by: Xah Lee | last post by:
Split File Fullpath Into Parts Xah Lee, 20051016 Often, we are given a file fullpath and we need to split it into the directory name and file name. The file name is often split into a core...
0
by: Xah Lee | last post by:
One-Liner Loop in Functional Style Xah Lee, 200510 Today we show a example of a loop done as a one-liner of Functional Programing style. Suppose you have a list of file full paths of...
7
by: lgbjr | last post by:
Hi All, I'm trying to split a string on every character. The string happens to be a representation of a hex number. So, my regex expression is (). Seems simple, but for some reason, I'm not...
20
by: Shawn Milo | last post by:
I'm new to Python and fairly experienced in Perl, although that experience is limited to the things I use daily. I wrote the same script in both Perl and Python, and the output is identical. The...
1
by: Arjun234 | last post by:
hi, I have a program to calculate the distance. its like this: open(IN, "/path/outModified.pl") or die "$!"; while (my $line = <IN>) { chomp($line); my @array = (split (/\s+/, $line)); #...
10
by: happyse27 | last post by:
Hi All, I got this apache errors(see section A1 and A2 below) when I used a html(see section b below) to activate acctman.pl(see section c below). Section D below is part of the configuration...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.