CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
9:5:4 => 09:05:04

 



ladycygnus
Novice

Aug 22, 2011, 7:31 AM

Post #1 of 9 (5700 views)
9:5:4 => 09:05:04 Can't Post

My idea is to do:

$time =~ s/(\D)(\d\D)/$10$2/g;

However this doesn't work. It sees $10 as an "undefined" variable instead of as a variable with a number after it. What am I missing?


FishMonger
Veteran / Moderator

Aug 22, 2011, 7:41 AM

Post #2 of 9 (5699 views)
Re: [ladycygnus] 9:5:4 => 09:05:04 [In reply to] Can't Post

Since you're only using 1 sets of capturing parans, only $1 and $2 will be defined.

If you're attempting to assign the var $10 prior to the regex, then that too is wrong and Perl should have given you a warning. Use $ten instead of $10.


ladycygnus
Novice

Aug 22, 2011, 7:57 AM

Post #3 of 9 (5695 views)
Re: [FishMonger] 9:5:4 => 09:05:04 [In reply to] Can't Post

I do not understand. I never want to use $10 - that would not be a proper variable since they should begin with a letter.

Here is my complete code, the reg ex is at the end.


Code
use strict; 
use warnings;

my $time = time();
print "Current Time; $time\n";
my $t_Batch1 = 1306416600;
print "Time of Batch 1 = $t_Batch1\n";

&printTime($t_Batch1);

sub printTime {
my $BatchTime = shift(@_);
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($BatchTime);
$year += 1900;
my @month_abbr = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my @week_abbr = qw( Sun Mon Tue Wed Thu Fri Sat );

my $string = "$week_abbr[$wday], $mday $month_abbr[$mon] $year $hour:$min:$sec +0000";

print "Before Change: $string\n";

### HERE IS THE SUBSTITUTION ###
$string =~ s/(\D)(\d\D)/$10$2/g;
print "After Change: $string\n";

}


This is the output I get:

Current Time; 1314024736
Time of Batch 1 = 1306416600
Before Change: Thu, 26 May 2011 9:30:0 +0000
Use of uninitialized value in concatenation (.) at C:\Documents and Settings\...\dateTest.pl line 24.
Use of uninitialized value in concatenation (.) at C:\Documents and Settings\...\dateTest.pl line 24.
After Change: Thu, 26 May 20119:300 +0000


ladycygnus
Novice

Aug 22, 2011, 8:00 AM

Post #4 of 9 (5694 views)
Re: [ladycygnus] 9:5:4 => 09:05:04 [In reply to] Can't Post

I want this:

$string =~ s/(\D)(\d\D)/$1a$2/g;

where a is equal to 0...hmm... {{does a quick test}}

This works:

Code
   my $zero = 0; 

$string =~ s/(\D)(\d\D)/$1$zero$2/g;
print "After Change: $string\n";


so...um thanks.


(This post was edited by ladycygnus on Aug 22, 2011, 8:01 AM)


FishMonger
Veteran / Moderator

Aug 22, 2011, 8:47 AM

Post #5 of 9 (5687 views)
Re: [ladycygnus] 9:5:4 => 09:05:04 [In reply to] Can't Post

Why on earth would you want to jump through those messy hoops just to format a datestamp?

Use the strftime function from the POSIX module.


Code
use strict;  
use warnings;
use POSIX;

my $time = time();
print "Current Time; $time\n";
print strftime("%a, %d %b %Y %H:%M:%S +0000", localtime($time) ), "\n\n";

my $t_Batch1 = 1306416600;
print "Time of Batch 1 = $t_Batch1\n";
print strftime("%a, %d %b %Y %H:%M:%S +0000", localtime($t_Batch1) ), $/;



ladycygnus
Novice

Aug 22, 2011, 8:50 AM

Post #6 of 9 (5685 views)
Re: [FishMonger] 9:5:4 => 09:05:04 [In reply to] Can't Post

Because I want to be able to run this script on other computers and the installation of perl we have is a very basic one. So much so that I have to remember to remove strict and warnings before sending it out...which I don't always remember, but my coworkers have gotten into the habit of recognizing that error and grumbling as they delete those lines.

But yes, I did look wistfully at that function before beginning this mess.


(This post was edited by ladycygnus on Aug 22, 2011, 8:52 AM)


FishMonger
Veteran / Moderator

Aug 22, 2011, 8:58 AM

Post #7 of 9 (5682 views)
Re: [ladycygnus] 9:5:4 => 09:05:04 [In reply to] Can't Post

I'm sorry, but that doesn't make sense.

If you develop and test your scripts properly, they will be strict and warnings safe. The strict and warnings pragmas should be in EVERY Perl script you write and there is no valid reason not to include them. If necessary, they can be disabled for certain sections of code, but that is rarely needed.

The POSIX module is a core module, which means it's included in all perl 5.x distributions.


FishMonger
Veteran / Moderator

Aug 22, 2011, 9:01 AM

Post #8 of 9 (5681 views)
Re: [ladycygnus] 9:5:4 => 09:05:04 [In reply to] Can't Post

If you really must use the messy approach, then use sprintf to format and add the leading zero instead of the regex.


ladycygnus
Novice

Aug 22, 2011, 9:22 AM

Post #9 of 9 (5676 views)
Re: [FishMonger] 9:5:4 => 09:05:04 [In reply to] Can't Post

I'm not a perl guru, I just hack together scripts to do basic tasks that I'm too lazy to do repeatedly. We are running windows machines (on which we are not allowed to install things on without permission) and one of the programs we use happens to have perl as a piece of it. My heart rejoiced at the sight, yet since it's a piece of the other program, it's a basic install for what that program needed and includes no modules.

I added the strict and warning modules to my installation because I know how important they are to writing the code - however once the code is written it doesn't matter if they are still there. I can't; however, write a code which uses modules which are not present in everyone else's build.

Because of this my codes generally reinvent the wheel 10x over. However, I've become pretty good at getting effective code...even if it would cause heart attacks among the gurus of the world.

I didn't know about sprintf! What a fun little function. That will work a lot better and simplify this a lot. Thanks Smile

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives