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

I'm trying to convert the string:

2000/0827
to:

08/27/2000
this isnt working:

#!/usr/local/bin/perl -w

use strict;


my $date_cfg;
my $new_date;
print "Enter date of config file using < yyyy/mmdd >: ";
chomp ( $date_cfg = <STDIN>);



until ( -e "/home/limo/Perl/$date_cfg/test") {

die "Error: $!\n";

    
}

$date_cfg =~ s!(d{4})/(d{4})!$2/$3/$1!;


print "Creating Juniper configuration file from: $date_cfg\n";

I also tried :

....snip

print "Creating Juniper configuration file from: $2/$3/$1\n"
Doesn't work.

Replies are listed 'Best First'.
Re: positional variable question
by btrott (Parson) on Aug 27, 2000 at 06:22 UTC
    You're missing the backslash before the "d" in your regex:
    \d{4}
    This regex does what you want, I think:
    $date_cfg =~ s!^(\d{4})/(\d{2})(\d{2})$!$2/$3/$1!;
    There may be a better way, but this works for me.
      In addition to the typo, "(\d{4}", that's what I was missing! Splitting the string into 2 variables and substituting them into 3! Thanks!
Re: positional variable question
by Anonymous Monk on Aug 27, 2000 at 06:54 UTC
    I think a regular expression is a bit overkill for the situation. You could probably do it much faster using unpack:
    my ($year,$month,$day) = unpack 'a4 x1 a2 a2', $date_cfg; my $new_date = "$month/$day/$year";
    or if you want to do it in one line:
    my $new_date = join '',(unpack 'a4 x1 a2 a2',$date_cfg)[1,2,0];
RE: positional variable question
by Anonymous Monk on Aug 27, 2000 at 13:28 UTC
    Escape the /'s (\/)
Re: positional variable question
by Anonymous Monk on Aug 28, 2000 at 01:21 UTC
    #!/usr/local/bin/perl # fourth time is the charm - i did not notice the posting instructions # i thought it was part of another perl question - sorry. use strict; my $date_cfg; my $new_date; print "Enter date of config file using : "; $date_cfg=<>; # you actually have to get the input chomp $date_cfg; #chomp off the lf $date_cfg =~ s!(\d{4})/(\d{2})(\d{2})!$2/$3/$1!; # year / month day mo/da/yr die "Wrong date config" unless ( -e "/home/limo/Perl/$date_cfg/test"); # not sure what the until was here - might have been part of a larger +construct # that u simplified for the example. print "Creating Juniper configuration file from: $date_cfg\n"; #hth, ___cliff rayman___cliff_at_rayman.com
Re: positional variable question
by Anonymous Monk on Aug 28, 2000 at 01:19 UTC
    #!/usr/local/bin/perl # third time is the charm - i hope this has proper formatting. use strict; my $date_cfg; my $new_date; print "Enter date of config file using : "; $date_cfg=<>; # you actually have to get the input chomp $date_cfg; #chomp off the lf $date_cfg =~ s!(\d{4})/(\d{2})(\d{2})!$2/$3/$1!; # year / month day mo/da/yr die "Wrong date config" unless ( -e "/home/limo/Perl/$date_cfg/test"); # not sure what the until was here - might have been part of a larger construct # that u simplified for the example. print "Creating Juniper configuration file from: $date_cfg\n"; #hth, ___cliff rayman___cliff_at_rayman.com
Re: positional variable question
by Anonymous Monk on Aug 27, 2000 at 11:06 UTC
    last message ended up being a perl onliner - but not intentionally. :-( #!/usr/bin/perl -dw use strict; my $date_cfg; my $new_date; print "Enter date of config file using : "; $date_cfg=<>; chomp $date_cfg; $date_cfg =~ s!(\d{4})/(\d{2})(\d{2})!$2/$3/$1!; die "Wrong date config" unless ( -e "/home/limo/Perl/$date_cfg/test"); print "Creating Juniper configuration file from: $date_cfg\n";
RE: positional variable question
by Anonymous Monk on Aug 27, 2000 at 11:03 UTC
    try this: #!/usr/local/bin/perl use strict; my $date_cfg; my $new_date; print "Enter date of config file using : "; $date_cfg=<>; # you actually have to get the input chomp $date_cfg; #chomp off the lf $date_cfg =~ s!(\d{4})/(\d{2})(\d{2})!$2/$3/$1!; # year / month day mo/da/yr die "Wrong date config" unless ( -e "/home/limo/Perl/$date_cfg/test"); # not sure what the until was here - might have been part of a larger construct # that u simplified for the example. print "Creating Juniper configuration file from: $date_cfg\n"; #hth, ___cliff rayman___cliff_at_rayman.com