Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
A substitution regular expression normally changes the string to edit and returns the number of changes. Thus you actually changed your target string for your second case. To get what you meant to get, you would use the code
#!/usr/bin/perl use strict; use warnings; my $adate = "2017-01-29 11:30:07.370"; # more direct way, but returning a "1". my $a_new_datetime = $adate; $a_new_datetime =~ s/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2})(.*)/$2-$ +3-$1 $4:$5/; print "\n 1 - $a_new_datetime\n\n"; my $new_datetime = $adate; $new_datetime =~ s/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2})(.*)/$2-$3- +$1 $4:$5/; print " 2 - $new_datetime\n\n";
Alternatively, you can use the r modifier (s/_PATTERN_/_REPLACEMENT_/msixpodualngcer in perlop)to shift the behavior to return a value without editing the original string:
#!/usr/bin/perl use strict; use warnings; my $adate = "2017-01-29 11:30:07.370"; # more direct way, but returning a "1". ; printf "\n 1 - %s\n\n", $adate =~ s/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):( +\d{2})(.*)/$2-$3-$1 $4:$5/r; printf " 2 - %s\n\n", $adate =~ s/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d +{2})(.*)/$2-$3-$1 $4:$5/r;

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

In reply to Re: Assigning a parsed date to a variable by kennethk
in thread Assigning a parsed date to a variable by Anonymous Monk

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2021-11-27 22:52 GMT
Find Nodes?
    Voting Booth?

    No recent polls found