Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

s// doesn't work when second loop!

by SoulRule (Initiate)
on Jan 10, 2018 at 16:08 UTC ( #1207071=perlquestion: print w/replies, xml ) Need Help??
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:
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; 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=~ ! +!
But when I put the initial string in while loop, It works fine
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 (Bishop) on Jan 10, 2018 at 16:56 UTC
    That's because after the replacement, the pattern no longer matches the string.
    #! /usr/bin/perl use warnings; use strict; my $input = 'a'; my $doesnt_work = $input; for (1 .. 10) { my $works = $input; $works =~ s/a/$_/; $doesnt_work =~ s/a/$_/; print "Works: $works\n", "Doesn't work: $doesnt_work\n"; }
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1207071]
Approved by marto
Front-paged by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2018-02-24 10:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    When it is dark outside I am happiest to see ...














    Results (310 votes). Check out past polls.

    Notices?