Gossamer Forum
Home : General : Perl Programming :

Do I use substr or s///g; ?

Quote Reply
Do I use substr or s///g; ?
Quick question: I've got a date 11/01/2001 and I need to keep the format "as is" except in one part where it's required to be 11/01/01.

Logic tells me I need to search for character positions 6 & 7 and replace them with nothing.

I've tried several variations of s///g; and substr(); but can't quite seem to get the syntax right.

If anyone is willing to point me in the right direction, I'd much appreciate it.

Thanks, Mike.

Quote Reply
Re: [Watts] Do I use substr or s///g; ? In reply to
Edit: Wouldnt work

Last edited by:

RedRum: Nov 1, 2001, 11:03 AM
Quote Reply
Re: [Watts] Do I use substr or s///g; ? In reply to
Ok this works (but its tooo long):

$date =~ s,^(\d{2}/\d{2}/)\d\d(\d{2})$,$1$2,;

Last edited by:

RedRum: Nov 1, 2001, 11:51 AM
Post deleted by Bmxer In reply to
Quote Reply
Re: [Watts] Do I use substr or s///g; ? In reply to
cant you do:
Code:
my $date = '11/01/2001';
$date =~ s;\d\d(\d\d)$;$1;

--Philip
Links 2.0 moderator
Quote Reply
Re: [PerlFreak] Do I use substr or s///g; ? In reply to
Nope. But you can do:

$date =~ s;\d\d(\d\d)$;$1;;

Angelic

Last edited by:

RedRum: Nov 1, 2001, 11:21 AM
Quote Reply
Re: [RedRum] Do I use substr or s///g; ? In reply to
Any reason not to use the substr function?

$date= '11/05/2001';
substr($date, -4, 2, '');
print $date;


Seems a little less confusing than a regex, IMHO. Smile

--
Matt G
Quote Reply
Re: [RedRum] Do I use substr or s///g; ? In reply to
Paul, that code I posted is valid... you should know that it's perfectly acceptable (even suggested in perldoc) to omit the ';' from the last line of code in a block. Mad

Besides, I think he would have added the ';' anyway.

--Philip
Links 2.0 moderator
Quote Reply
Re: [PerlFreak] Do I use substr or s///g; ? In reply to
Yes Drew I realise you can omitt ; on some occasions but I also tried the code and it gave me syntax errors which disappeared when I added the extra ;
Quote Reply
Re: [mglaspie] Do I use substr or s///g; ? In reply to
less confusing?? I don't think so. what if your variable isn't defined directly above your substr statement? how can you know, without scrolling through your source code, whta exactly you're searching for and replacing? That's why I'd prefer regexps... even if reading them is a bit cryptic at times.

--Philip
Links 2.0 moderator
Quote Reply
Re: [PerlFreak] Do I use substr or s///g; ? In reply to
Give the following code a try...you'll see what I mean Cool

Code:
#!/perl/bin/perl

use CGI::Carp qw(fatalsToBrowser);

print "Content-type: text/plain\n\n";

my $date = '11/01/2001';
$date =~ s;\d\d(\d\d)$;$1;


print $date;
Quote Reply
Re: [RedRum] Do I use substr or s///g; ? In reply to
I don't get any errors when I run this:
Code:
$date = '11/01/2001';
{
$date =~ s;\d\d(\d\d)$;$1;
}
print $date
which is what I meant by:
Quote:
it's perfectly acceptable (even suggested in perldoc) to omit the ';' from the last line of code in a block

--Philip
Links 2.0 moderator

Last edited by:

PerlFreak: Nov 1, 2001, 12:03 PM
Quote Reply
Re: [PerlFreak] Do I use substr or s///g; ? In reply to
Me neither but that wasn't what you originally posted.
Post deleted by PerlFreak In reply to

Last edited by:

PerlFreak: Nov 1, 2001, 12:09 PM
Quote Reply
Re: [PerlFreak] Do I use substr or s///g; ? In reply to
Don't get me wrong, I'm a big fan of regex's... I use them far more often than substr (which is almost never).

But in this particular application, you know that you are dealing with a four digit year at the end of a string... seems the easiest thing to do is go back four spaces and replace two characters with nothing... which is almost exactly what you are doing with the regex.

I see your point about being able to decipher a regex to see what you are searching/replacing... something of a tradeoff in either case, I suppose. I guess it would depend on the complexity of the script.

Smile
--
Matt G
Quote Reply
Re: [PerlFreak] Do I use substr or s///g; ? In reply to
I'm beginning to get a complex with all these DUH's aimed at me :(

Guess I'm just inferior PiratePirate
Quote Reply
Re: [mglaspie] Do I use substr or s///g; ? In reply to
Quote:
substr($date, -4, 2, '');

and the four argument version of substr doesn't work in perl 5.004 which means you have to use the cryptic:

substr($date, -4, 2) = "";

;)

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Do I use substr or s///g; ? In reply to
Wow! What fun! Thanks for the ideas...

Y'all solved my problem.

Smile