Chas. Owens wrote:
On Sun, Feb 8, 2009 at 03:49, Gunnar Hjalmarsson <nore...@gunnar.cc> wrote:
Sorry, but I fail too see how using the s/// operator to extract the date
field would be so much more confusing and fragile compared to split() +
join().

You are calling three functions (one of which is split) and assigning
returns three times inside the replacement.  Add on top of that the
fact that the regex only works for the second field.  Compare all of
that to calling two much simpler functions, a simple substitution, and
one assignment.

Think you are comparing apples and oranges now. Since we don't know what kind of conversion the OP wants to do, I thought we were only discussing the date extracting part of the problem. To clarify, I rewrote my code:

use Time::Local;

while (<DATA>) {
    s{(?<=,)(.+?)(?=,)}{ dateconvert($1) }e;
    print;
}

sub dateconvert {
    my ($d, $m, $y) = split /\//, shift;
    my $t = timelocal 0, 0, 0, $d, $m-1, $y;
    ($d, $m, $y) = (localtime $t)[3..5];
    sprintf '%d-%02d-%02d', $y+1900, $m+1, $d;
}

__DATA__
<TICKER>,06/02/09,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOLUME>,<OI>
<TICKER>,07/02/09,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOLUME>,<OI>
<TICKER>,08/02/97,<OPEN>,<HIGH>,<LOW>,<CLOSE>,<VOLUME>,<OI>


In other words, if we are to compare each others code, I believe that

    s{(?<=,)(.+?)(?=,)}{ dateconvert($1) }e;
    print;

ought to be compared with

    my @record = split /,/, $_;
    $record[1] = dateconvert( $record[1] );
    print join ",", @record;

Try to imagine what happens six months from now when
you need to go back and perform a transformation on the fifth field.
Are you going to extend the regex to try to capture that value?  Or
are you just going to rewrite the code to use a split like you should
have in the first place?

Didn't think about that. Maybe I will use split + join. Not a big deal, IMO.

I am all for using advanced features of Perl when it makes the code
clearer or more concise, but this code is longer than the split
version, involves more functions (including the confusing* localtime
and timelocal functions),

My use of localtime and timelocal is totally unrelated to whether I use "the split version" or not.

and doesn't even do error checking on the data.

Not true. timelocal() does error checking.

On an unrelated topic, why are you using timelocal?

Because of its built-in error checking? ;-) Or maybe because I wanted to use its "Year Value Interpretation" feature. (Note that I assumed conversion from dd/mm/yy to yyyy-mm-dd, and that a date from the 90's is included in my sample data.)

A much better
solution is to use the strftime function from the POSIX module:

Maybe.

Somehow I tend to believe that date conversion code becomes more robust if you go to epoch seconds and back. Isn't that what most date and time related modules do behind the scenes, btw?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to