Forum Moderators: coopster & phranque

Message Too Old, No Replies

Splitting a line

Can someone show me where i'm going wrong?

         

bigshow

2:04 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



17/06/05 15:15:33 - 00 - (abcdefghijklm) Have recieved END message.

my ($dt,$mon,$yr,$hr,$min,$sec,$restofit) = split(\d+?/\d+?/\d+?\s+\d+?\:\d+?[\w?¦\s?],$line);

where
\d+?/\d+?/\d+? is date,month & year
\s+ is space
\d+?\:\d+? is hh:mi
[\w?¦\s?] is word or spaces, i.e. the rest of the string.

Can somebody show me what the split command should be?

Moby_Dim

3:42 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



split(/\d+?\/\d+?\/\d+?\s+\d+?:\d+?[\w?¦\s?]/,$line)

Moby_Dim

3:52 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



Actually, there was just syntax, and now regexp issue :

/\d+?\/\d+?\/\d+?\s+\d+?:\d+?[\w?¦\s?]/.

May be you need not pipe char at all, and the tail of the regexp should be : \d+[\w\s]+ OR \d+[^\d]+ (if no digits in the tail) OR \d+\s+.+

Moby_Dim

4:23 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



oh - we all need to think first ;) Here no split needed

($dt,$mon,$yr,$hr,$min,$sec,$restofit)=$line=~m/^(\d+?)\/(\d+?)\/(\d+?)\s+(\d+?):(\d+?):(\d+?)(.+)$/;

:( means : and (

ckarg

8:23 am on Jun 29, 2005 (gmt 0)

10+ Year Member



The choice between split and m// comes down to whether you focus on what you don't want (use split) or what you want (use m//).

If you're only interested in the numbers, you could use:

($dt,$mon,$yr,$hr,$min,$sec) = split /\D+/, $line;

This splits the line on 'one or more non-digits', giving a list of all numbers.

The other way would be:

($dt,$mon,$yr,$hr,$min,$sec) = $line =~ m/(\d+)/g

The g modifier applies the regex 'one or more digits' to $line globally, and returns an array of all matches.

Moby_Dim

11:13 am on Jun 29, 2005 (gmt 0)

10+ Year Member



This does not guarantees and could read the wrong formatted line or other line with digits. I'd recommend even use not \d+ , but \d{n}