Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re^4: Space between digits

by Anonymous Monk
on Nov 21, 2012 at 21:48 UTC ( [id://1005018]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Space between digits
in thread Space between digits

The code you gave is not working I tried this
if ($_ =~ m/(\d+)\:(\d+) (\d+)\/(\d+)\/(\d+)/ ){ my $string = "(\d+)\:(\d+) (\d+)\/(\d+)\/(\d+)"; my $new = $string =~ s/ / /; print "new $new "; }

Replies are listed 'Best First'.
Re^5: Space between digits
by muba (Priest) on Nov 22, 2012 at 01:35 UTC
    my $string = "(\d+)\:(\d+) (\d+)\/(\d+)\/(\d+)";

    What is the purpose of this line? Does it really help you to do what you want to? What happens if you leave it out entirely?

    s/// works in place (that is, on the variable it is bound to). What happens if you rewrite it as this:

    if ($_ =~ m/(\d+)\:(\d+) (\d+)\/(\d+)\/(\d+)/ ){ $_ =~ s/ / /; }

    And of course, m// and s/// works on $_ in lieu of any other variable, so you could even just go:

    if (m/(\d+)\:(\d+) (\d+)\/(\d+)\/(\d+)/ ){ s/ / /; }

    Which could be shortend to:

    s/ / / if m/(\d+)\:(\d+) (\d+)\/(\d+)\/(\d+)/;

    Those parens serve no purpose, as you're seemingly not capturing anything, so we can be even more briefer without harming readability:

    s/ / / if m/\d+\:\d+ \d+\/\d+\/\d+/;
          s/ /   / if m/\d+\:\d+ \d+\/\d+\/\d+/;

      And because substitution is already conditional upon a match, the operation can easily be further briefened (if that's a word) and also made more specific and at the same time more general or global:

      >perl -wMstrict -le "my $string = '9 9 0:00 11/21/2012 12:34 01/23/2012 9 9'; print qq{'$string'}; ;; $string =~ s{ \d{1,2}:\d{2} \K [ ] (?= \d{2}/\d{2}/\d{2}) } { }xmsg; print qq{'$string'}; " '9 9 0:00 11/21/2012 12:34 01/23/2012 9 9' '9 9 0:00 11/21/2012 12:34 01/23/2012 9 9'

      The stage is now set to factor out the definitions of time and date, etc., so they can easily be made more specific, perhaps ultimately being able to reject things like a 'time' of 99:99 or a 'date' of 98/76/5432, or accept a date in the form of either 01/23/2012 or 2012/01/23, etc.

      >perl -wMstrict -le "my $string = '9 9 0:00 11/21/2012 12:34 01/23/2012 9 9'; print qq{'$string'}; ;; my $time = qr{ (?<! \d) \d{1,2} : \d{2} (?! \d) }xms; my $date = qr{ (?<! \d) \d{2} / \d{2} / \d{4} (?! \d) }xms; ;; $string =~ s{ $time \K [ ] (?= $date) }{ }xmsg; print qq{'$string'}; " '9 9 0:00 11/21/2012 12:34 01/23/2012 9 9' '9 9 0:00 11/21/2012 12:34 01/23/2012 9 9'
Re^5: Space between digits
by tobyink (Canon) on Nov 23, 2012 at 16:06 UTC

    You tried that. That is not working. But that is not the code I posted.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1005018]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-03-19 06:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found