Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hey everyone .. can anyone tell me how to do a split on the following line DATETIME 09:59:11 I want to do split which will do it on white space and the : basically i want to get is 09 can anyone help thanks

Replies are listed 'Best First'.
Re: split help
by pzbagel (Chaplain) on Sep 22, 2003 at 17:32 UTC

    If all you want is 09, then just ask for it:

    /^DATETIME (\d\d):/; print "$1\n";

    HTH

Re: split help
by Plankton (Vicar) on Sep 22, 2003 at 17:32 UTC
    This is based on the Camel book's description of split. Its a good book maybe you should get yourself a copy.
    #!/usr/local/bin/perl my $data='DATETIME 09:59:11'; my (@sout) = split /[\s,:]/, $data; print $sout[1];

    Plankton: 1% Evil, 99% Hot Gas.

      Plankton, great answer! Now you can use a array slice to throw away what you don't need:

      #!/usr/local/bin/perl my $data='DATETIME 09:59:11'; my $sout = (split /[\s,:]/, $data)[1]; print $sout ."\n";

      Later

Re: split help
by thinker (Parson) on Sep 22, 2003 at 17:36 UTC

    Hi AM,

    This should do what you want. See perldoc -f split

    #!/usr/bin/perl use strict; use warnings FATAL => 'all'; my $str = "DATETIME 09:59:11"; my $time = (split " ", $str)[1]; my ($h,$m,$s) = split ':', $time; print $h;

    Hope this helps

    thinker

    Update Answered the question that was asked, not the one I imagined :-)
      Somewhat daft but I notice that no one has mentioned the third argument to split yet:

      use strict; use warnings FATAL => 'all'; # ;) my( $one, $two ) = split /:/, q[DATETIME 09:59:11], 2; print substr( $one, -2 );

      From perldoc -f split:
      If LIMIT is specified and positive, it represents the maximum number of fields the EXPR will be split into, though the actual number of fields returned depends on the number of times PATTERN matches within EXPR.