CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
String: Doesn't want to split at 4th occurrence

 



mhassan
Novice

Jan 18, 2010, 10:22 AM

Post #1 of 2 (341 views)
String: Doesn't want to split at 4th occurrence Can't Post

Hi, I am trying to use split function to split "inbound 2242325546 15 [18/Jan/2010:10:14:27 -0500] 6177123999x1892 000022;015;1263826563 1263827667 1263827710 0:43
". This is the first line from the test.txt. But I don't want to split at 4th space. Any ideas? So the array should be like..

1. inbound
2. 2242325546
3. 15
4. [18/Jan/2010:10:14:27 -0500]
5. 6177123999x1892
6. 000022;015;1263826563
7. 1263827667
8. 1263827710
9. 0:43


Code
open(FH,"test.txt"); 
while(<FH>) {
chomp;
my @line_array = split(' ');
foreach (@line_array) {
print $_ . "\n";
}
print OFH join('\t',@line_array);
}



7stud
Enthusiast

Jan 18, 2010, 8:10 PM

Post #2 of 2 (325 views)
Re: [mhassan] String: Doesn't want to split at 4th occurrence [In reply to] Can't Post


Code
my @pieces = split /\s(?!-)/, $str;


The (?! ) part is a 'zero width negative lookahead'. It matches any character but the one listed--in this case a minus sign. But very importantly, once the match is found, it is not treated as part of the result match. In other words, the regex above will look for a space followed by anything but a minus sign and then split on only the space.

Of course, you have given NO information about the pattern of your data. Whether that will work for all your data is impossible to tell. One example is meaningless.


(This post was edited by 7stud on Jan 18, 2010, 10:17 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives