regular expression format string in one line.

Hi All,

@months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
$day=091023;
$day_combine = $day;
$day_combine =~ s/([0-9]{2})([0-9]{2})([0-9]{2})/20$1-$months[$2-1]-$3/;

Instead of three lines, is possible to combine the last two lines into a single line? means no need assign $day to $day_combine first, just one line. :confused:

Thanks

Sure it's possible:

$
$ cat -n tst1.pl
     1  #!/usr/bin/perl
     2  @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
     3  $day="091023";
     4  ($day_combine = $day) =~ s/([0-9]{2})([0-9]{2})([0-9]{2})/20$1-$months[$2-1]-$3/;
     5  print $day_combine,"\n";
     6
$
$ perl tst1.pl
2009-Oct-23
$
$

And, taking it a step further, why not remove the middle-man completely ? :wink:

$
$ cat -n tst2.pl
     1  #!/usr/bin/perl
     2  @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
     3  $day_combine="091023";
     4  $day_combine =~ s/([0-9]{2})([0-9]{2})([0-9]{2})/20$1-$months[$2-1]-$3/;
     5  print $day_combine,"\n";
     6
$
$ perl tst2.pl
2009-Oct-23
$
$

tyler_durden

Hi tyler,
Thanks, i never thought can do ($day_combine = $day) =~ . :b:
By the way, i can not remove the middle-man, bec i need $day="091023"; this variable to be there.

Thanks