in reply to Better way to perform "double split" ?

Is there a better way to extract the date from the table?

How about:

use strict; use warnings; use 5.010; my $str = 'Timestamp 10.72.218.82:cpu_busy ---------------------------------- 2009-11-05 17:59:52 1.501 '; my ($date) = $str =~ /(^\d{4}-\d{2}-\d{2})/m; say "-->$date<--"; --output:-- -->2009-11-05<--

Replies are listed 'Best First'.
Re^2: Better way to perform "double split" ?
by 7stud (Deacon) on Nov 10, 2009 at 09:37 UTC

    By way of explanation:

    my ($date)

    provides a 'list context'. In other words, $date is part of a list of variables, where the list happens to be of length 1. That list of variables expects to be assigned a list of values. In response to that demand, the match operator m/// returns a list of of the actual matches to the parenthesized groupings in the regex.

    If m/// happened to return more than one value, because there were multiple parenthesized groupings in the regex, then the rules of list assignment would take over: extra values on the right hand side of a list assignment are discarded. Here is an example:

    use strict; use warnings; use 5.010; my ($a, $b) = (1, 2, 3); say $a; #1 say $b; #2
      Oh, yeah....the 'm' flag allows the ^ to match at the start of every line in the string.