Daniel Kasak schreef:

> I'm trying to split a date where the values can be separated by a dash
> '-' or a slash '/', eg:
> 2006-10-31 or 2006/10/31
>
> I'm using:
> my ( $yyyy, $mm, $dd ) = split /(-|\/)/, $yyyymmdd;
> but it doesn't work.

It does work, but not as you expected.
Read `perldoc -f split` again, look for "parentheses".

perl -wle'
  $_ = "1-2/3";
  print for split "(-|/)"
'

perl -wle'
  $_ = q{1-2/3};
  print for split /[[:punct:]]/
'

perl -wle'
  $_ = q{1-2/3};
  print for m/([0-9]+)/g
'


With a backreference, you can have it only match when the delimiters are
the same:

perl -wle'
  $_ = q{1-2-3};
  my ($yyyy, $delim, $mm, $dd) = m/([0-9]+)(.)([0-9]+)\2([0-9]+)/;
  print for ($delim, $yyyy, $mm, $dd)
'

-- 
Affijn, Ruud

"Gewoon is een tijger."


-- 
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