SoulRule has asked for the wisdom of the Perl Monks concerning the following question:
HI monks
I found the s/// doesn't work after replace once. look at below code:But when I put the initial string in while loop, It works fineuse strict; use warnings; use DateTime; use DateTime::Set; use DateTime::Format::Natural; use DateTime::Format::Oracle; $ENV{'NLS_DATE_FORMAT'} = 'DD.MM.YYYY HH24:MI'; my $step = 1; # one day one jump my $dt_from = DateTime->new( year => 2017, month => 10, day => 16, hour => 16, time_zone => 'Asia/Taipei', ); my $dt_to = DateTime->new( year => 2017, month => 10, day => 17, hour => 16, time_zone => 'Asia/Taipei', ); my $dt_next = $dt_from->clone->add(hours => $step); $dt_next = $dt_to->clone if $dt_next > $dt_to; my $indivparam_string = 'AutoE=1~AutoS=A~AutoT=1~TFrom=29.12.2017 06:4 +0:30.000 SDT Greenwich Standard Time~TTill=29.12.2017 16:00:00.000 SD +T Greenwich Standard Time~PLSType=0~GroupID=5922~ChkGrp=1~JobDefRegis +terSets=~'; while( $dt_next <= $dt_to){ my $string_from = DateTime::Format::Oracle->format_datetime($dt_from); my $string_next = DateTime::Format::Oracle->format_datetime($dt_next); print " from $string_from to $string_next \n"; print "old indiv string is $indivparam_string !!\n"; ###########################################just for some case, you hav +e to modify conn string directly##################### $indivparam_string =~ s/From=\K\d\d\.\d\d\.\d{4}\s\d\d:\d\d:\d\d/$stri +ng_from/; $indivparam_string =~ s/Till=\K\d\d\.\d\d\.\d{4}\s\d\d:\d\d:\d\d/$stri +ng_next/; print "new indiv string is $indivparam_string !!\n"; last if $dt_next == $dt_to; $dt_from = $dt_next->clone; $dt_next->add(hours => 1); $dt_next=$dt_to if $dt_next->clone->add(hours => 1) > $dt_to; } #output from 17.10.2017 07:00 to 17.10.2017 08:00 old indiv string is AutoE=1~AutoS=A~AutoT=1~TFrom=16.10.2017 08:00.000 + SDT Greenwich Standard Time~TTill=16.10.2017 09:00.000 SDT Greenwich + Standard Time~PLSType=0~GroupID=5922~ChkGrp=1~JobDefRegisterSets=~ ! +! new indiv string is AutoE=1~AutoS=A~AutoT=1~TFrom=16.10.2017 08:00.000 + SDT Greenwich Standard Time~TTill=16.10.2017 09:00.000 SDT Greenwich + Standard Time~PLSType=0~GroupID=5922~ChkGrp=1~JobDefRegisterSets=~ ! +!
use strict; use warnings; use DateTime; use DateTime::Set; use DateTime::Format::Natural; use DateTime::Format::Oracle; $ENV{'NLS_DATE_FORMAT'} = 'DD.MM.YYYY HH24:MI'; my $step = 1; # one day one jump my $dt_from = DateTime->new( year => 2017, month => 10, day => 16, hour => 16, time_zone => 'Asia/Taipei', ); my $dt_to = DateTime->new( year => 2017, month => 10, day => 17, hour => 16, time_zone => 'Asia/Taipei', ); my $dt_next = $dt_from->clone->add(hours => $step); $dt_next = $dt_to->clone if $dt_next > $dt_to; while( $dt_next <= $dt_to){ my $string_from = DateTime::Format::Oracle->format_datetime($dt_from); my $string_next = DateTime::Format::Oracle->format_datetime($dt_next); print " from $string_from to $string_next \n"; my $indivparam_string = 'AutoE=1~AutoS=A~AutoT=1~TFrom=29.12.2017 06:4 +0:30.000 SDT Greenwich Standard Time~TTill=29.12.2017 16:00:00.000 SD +T Greenwich Standard Time~PLSType=0~GroupID=5922~ChkGrp=1~JobDefRegis +terSets=~'; print "old indiv string is $indivparam_string !!\n"; ###########################################just for some case, you hav +e to modify conn string directly##################### $indivparam_string =~ s/From=\K\d\d\.\d\d\.\d{4}\s\d\d:\d\d:\d\d/$stri +ng_from/; $indivparam_string =~ s/Till=\K\d\d\.\d\d\.\d{4}\s\d\d:\d\d:\d\d/$stri +ng_next/; print "new indiv string is $indivparam_string !!\n"; last if $dt_next == $dt_to; $dt_from = $dt_next->clone; $dt_next->add(hours => 1); $dt_next=$dt_to if $dt_next->clone->add(hours => 1) > $dt_to; }
Please help me! Thanks in advance.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: s// doesn't work when second loop!
by choroba (Cardinal) on Jan 10, 2018 at 16:56 UTC |