For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > July 2005 > Control Structure Question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Control Structure Question
Dave Adams

2005-07-24, 8:29 pm

The following code looks too rigid, how can I achieve the same result
using one of the control structure (foreach, for, etc)?


#!/usr/bin/perl -w
use strict;
use Date::Calc qw(:all);

my @today =3D Today();
my $year_now =3D $today[0];
my $month_now =3D $today[1];
my $day_now =3D $today[2];

print ($year_now);
print ("=3D=3D=3D\n");
print ($month_now);
print ("=3D=3D=3D\n");
print ($day_now);
print ("=3D=3D=3D\n");
Jeff 'japhy' Pinyan

2005-07-24, 8:29 pm

On Jul 22, Dave Adams said:

> The following code looks too rigid, how can I achieve the same result
> using one of the control structure (foreach, for, etc)?


> my @today = Today();
> my $year_now = $today[0];
> my $month_now = $today[1];
> my $day_now = $today[2];


Well, first of all, that could just be:

my ($year_now, $month_now, $day_now) = Today();

> print ($year_now);
> print ("===\n");
> print ($month_now);
> print ("===\n");
> print ($day_now);
> print ("===\n");


And those could be:

print "$_===\n" for $year_now, $month_now, $day_now;

And if Today() only returns those three values, you could do:

print "$_===\n" for Today();

If Today() returns more than three values, then you could do:

print "$_===\n" for (Today())[0..2];

--
Jeff "japhy" Pinyan % How can we ever be the sold short or
RPI Acacia Brother #734 % the cheated, we who for every service
http://japhy.perlmonk.org/ % have long ago been overpaid?
http://www.perlmonks.org/ % -- Meister Eckhart
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com