Re: date format



Ian Wilson wrote:

Bart Van der Donck wrote:
[...]
#!perl
use strict;
use warnings;
my $file =
'%%%%%%%% HEADER Fri Jun 16 17:21:03 MEST 2006
%%%%%%%%%%%%
1111111111111111111111
2222222222222222222222
3333333333333333333333
';
my @m = ('Jan', 'Feb', 'Mar', 'Apr', 'May',
'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
my @ar = split / /, (split /\n/, $file)[0];
$ar[3] =~ s/$m[$_]/@{[sprintf('%02g',$_+1)]}/ for (0..$#m);
die "Can't find month '$ar[3]'\n" unless $ar[3]=~/^(0|1)[0-9]$/;
print $ar[7], $ar[3], sprintf('%02g',$ar[4]);

__END__

Hope this helps,

I'm not a beginner and I found this example rather obscure and hard to
understand. Perl needn't look that much like line noise IMO.

I think you've entered the domain of personal preferences here. As far
as I'm concerned, my code doesn't read as line noise (I can easliy
follow it, but hey, that's me).

I for one have more difficulty to follow Mr Krahn's code, as I'm not
too confident with some of his techniques... But I do think that his
code will be perceived as more obscure by a beginner when you juxtapose
it next to mine.

But of course I do like occasional "sport lines" like

$ar[3] =~ s/$m[$_]/@{[sprintf('%02g',$_+1)]}/ for (0..$#m);

in stead of splitting it up in 5 lines or so, like I would rather do in
production code. Just do myself a favour in the future :-)

I'd put the data in a DATA section since it gives a structure
'while(<>){}' that is closer to the way most peaple read data files.

I agree, that would have been better indeed.

I'd use meaningful variable names since the OP claims to be a beginner
(though I think they're a good idea no matter your level of experience)

$file = file
@ar short for array (which isn't descriptive for its content of course,
but the content of that array is not univocal here anyway)
@m short for @months

Well, yes, when written in full they would become more meaningful I
suppose...

[...]

--
Bart

.



Relevant Pages

  • Re: Plz HELP
    ... I am a student who is working on Perl on my project. ... I am a beginner in Perl. ... I know how to read the data from files into a variable or array. ... Use the substitution operator and the hex() function ...
    (perl.beginners)
  • Plz HELP
    ... I am a student who is working on Perl on my project. ... I am a beginner in Perl. ... I know how to read the data from files into a variable or array. ...
    (perl.beginners)
  • Re: Learning Perl
    ... it should be an array, ... Then they'd be completely inaccessible to beginners. ... that should be my $var. ... so why is it redundant to point out that Perl is different from C here? ...
    (comp.lang.perl.misc)
  • Re: Beginner needs help with script :)
    ... >> come to these forums and post a question about perl AT ALL? ... Yes what he was saying was that the subject matter in my subject line, ... And I put it to him and you that my subject line simplysays I am a beginner ... is very honest and humble I think. ...
    (comp.lang.perl.misc)
  • Re: Which is faster - hash or array lookup
    ... I am suite sure that perl is not The Right Tool ... Each player has 5 pieces, and additionally there is a common ... whereas traversing an array looking for $n is slow). ...
    (comp.lang.perl.misc)