Am Donnerstag, 31. März 2005 11.08 schrieb John:
> hello
>
> I want to split the datetime of apache log
>
> i tried this code with no success
>
> @fields=split(/[/:]/, $datetime); ### DD/MM/YYYY:HH:MM:SS
>
> has anyone tried anything like that?

Always use "use strict; use warnings" in your code - it gives you very good 
hints:

use strict; 
use warnings;
my $datetime="DD/MM/YYYY:HH:MM:SS";
@fields=split(/[/:]/, $datetime);

# This prints out:
Global symbol "@fields" requires explicit package name at - line 3.
Unmatched [ in regex; marked by <-- HERE in m/[ <-- HERE / at - line 3.

Here is the modified code:

use strict; use warnings;
my $datetime="DD/MM/YYYY:HH:MM:SS";
my @fields1=split(/[\/:]/, $datetime); # [1]
my @fields2=split(m,[/:],, $datetime); # [2]
print join ", ", @fields1;
print "\n";
print join ", ", @fields2;

# This prints out:
DD, MM, YYYY, HH, MM, SS
DD, MM, YYYY, HH, MM, SS

To avoid perl interpreting a "/" searched for with the end of the regex, you 
either have to 
[1] escape "/" with " \"
or 
[2] use another "delimiter" (don't know the english term at the moment) for 
the match regex - in this case, "m,," instead of "//", explicitly indicating 
the match with an "m".

see perldoc perlre

joe

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to