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: Regular Expressions:
Doesn't want to split at 4th occurrence

 



mhassan
Novice

Jan 18, 2010, 11:06 AM

Post #1 of 3 (2749 views)
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);
}



FishMonger
Veteran / Moderator

Jan 18, 2010, 11:32 AM

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

You need to use a Negative lookahead zero width assertion.


Code
while(my $line = <FH>) { 
chomp $line;
my @line_array = split(/\s(?!-)/, $line);
foreach (@line_array) {
print $_ . "\n";
}
print OFH join('\t',@line_array);
}



mhassan
Novice

Jan 18, 2010, 11:37 AM

Post #3 of 3 (2740 views)
Re: [FishMonger] Doesn't want to split at 4th occurrence [In reply to] Can't Post

Thanks it works!

 
 


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

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