On 11/21/05, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:

> [EMAIL PROTECTED] wrote:
>
> >In my situation I have a string like so:
> >
> >my $string = '20050516';
> >
> >and I want to rearrange it to look like: 05162005, so I tried:
> >
> >my $newstring =
> >substr($string,0,8).substr($string,0,4).substr($string,8,0);
> >
> >
> >But my problem is: it's leaving 2005 in the beginning of the string to
> look
> >like 200505162005 when all I need is 05162005
> >Any ideas?
> >
> >
> my $newstring = substr($string,4,4).substr($string,0,4);
>
> Should do the trick...

> However, is one way more efficient that the other. To me Roberto's way
> seems easier to read compared to Jeff's:
>
> substr($string, 4, 2) . substr($string, 6, 2) . substr($string, 0, 4)

It all depends on what you want.

Jeff very nicely replicated what you were trying to do, that is,
separate out the year, month, and day, and rearrange them to your
liking.

Roberto, on the other hand, gave you code to match your end result,
not your process.

Roberto's is more efficient--there's one less substring function on
each pass, which may or may not make a difference if you're doing
thousands of passes. Jeff's on the other hand, more closely mirrors
what you were trying to do yourself. Obvously at some point you though
it was important to handle the three pieces of the date separately,
and Jeff's code is more flexible.

Which makes most sense for your current application only you know.

If lots of dates are in your future--and they probably are; dates are
a common problem--what you probably really want to do is take a look
at Date::Calc, Date::Manip, Date::Parse, etc.

HTH,

-- jay
--------------------------------------------------
This email and attachment(s): [  ] blogable; [ x ] ask first; [  ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com  http://www.dpguru.com  http://www.engatiki.org

values of β will give rise to dom!

Reply via email to