LPM: RE: join

Rich Bowen rbowen at rcbowen.com
Sun Apr 2 21:07:08 CDT 2000


repett0 at sac.uky.edu wrote:
> 
> thanks, now I see the problem (I think :)) you need to use brackets.  I
> assumed that you get one line for free like c/c++ or java.  One more ?,
> how do you get the length.  Im doing it like this now...
> 
> #!/usr/local/bin/perl
> 
> for($i = 0; $i < 10; $i++)
> {    $foo[$i] = int(rand(10));print "$foo[$i]\n";}

That works.

-OR-

$foo[$_] = int(rand(10)) and print "$foo[$_]\n" for (1..10);

But then, I'm usually an advocate of readability, and so I'd probably
write it as

for (1..10)	{
	$foo[$_] = int(rand(10));
	print "$foo[$_]\n";
}

Or, better yet,

foreach $i (1..10)	{
	$foo[$i] = int(rand(10));
	print "$foo[$i]\n";
}

After all, TIMTOWTDI _is_ the motto of Perl.

I never use the "for (;;)" syntax if I can avoid it. Somehow, it seems
rather cumbersome to me. But it might be more comfortable to a C
programmer.

Rich
-- 
http://www.ApacheUnleashed.com/
Lexington Perl Mongers - http://lexington.pm.org/
PGP Key - http://www.rcbowen.com/pgp.txt



More information about the Lexington-pm mailing list