On Monday 17 December 2007 15:40, [EMAIL PROTECTED] wrote:
>
> On Dec 17, 3:22 am, [EMAIL PROTECTED] (Pauld) wrote:
> >
> > my $var=0;my [EMAIL PROTECTED];
> > while ($var<$va_length)
> > {
> > print "${$daylistsorted[$var]}{TH} ";
> > print 'from ';
> > print ${$daylistsorted[$var]}{START};
> > print ' to '.${$daylistsorted[$var]}{END_DS};
> > print " duration ";print int((${$daylistsorted[$var]}{END}-$
> > {$daylistsorted[$var]}{START})/60);
>
> It's unusual in Perl to need to access an array element by its index
> number. This is one of those times, though, when it is useful to use
> an index because you need to peek ahead at the next item in the
> array. But you only need the index for the next item, not for the
> current item, so you can clean up things a bit with something like
> this (untested, and posted without much effort to parse or understand
> the objective of the code, and using printf instead of a bunch of
> concat'ed strings):
>
> my $index = 0;
> foreach my $day( @daylistsorted ) {
The way it is *usually* done is:
foreach my $index ( 0 .. $#daylistsorted ) {
> printf (
> "%s from $s to %s duration %s %s\n",
> $day{'TH'},
An array element can hold a hash reference but not a hash itself:
$day->{ TH },
> UnixDate($day{'START'}, '%Y:%m:%d %H:%M'),
$day->{ START }
> UnixDate($day{'END_DS'}, '%Y:%m:%d %H:%M'),
$day->{ END_DS }
> int(($day{END} - $day{START})/60);
$day->{ END } - $day->{ START }
> (exists( ${$daylistsorted[$index+1]}{TH} ) )
> ? "\tinterval to next start "
> .int (( ${$daylistsorted[$index+1]}{START}
> -$day{END} )/60)
$day->{ END }
>
> : ''
>
> );
> $index++;
> }
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/